wjc
9 小时以前 41fc4df45eb9c43560a9660188a614ede0072200
app/src/main/java/com/hdl/photovoltaic/uni/MyForegroundService.java
@@ -14,6 +14,7 @@
import com.hdl.photovoltaic.R;
public class MyForegroundService extends Service {
    private static final String CHANNEL_ID = "MyForegroundServiceChannel";
    private static final int NOTIFICATION_ID = 1;
@@ -26,15 +27,30 @@
        // 创建通知渠道(Android 8.0+)
        createNotificationChannel();
        isRunning = true;
    }
    }
//    @Override
//    public void onTaskRemoved(Intent rootIntent) {
//        super.onTaskRemoved(rootIntent);
//        isRunning = false;
    /// /        // 停止前台状态,移除通知
    /// /        stopForeground(true);  // true表示移除通知
    /// ///        // 停止服务
    /// ///        stopSelf();
//        // 1. 先移除前台状态(可选,但建议)
//        stopForeground(true);
//
//        // 2. 停止服务
//        stopSelf();
//    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 创建通知
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("")//宿主进程服务
                .setContentText("")//正在与uni-app保持通信
                .setSmallIcon(R.drawable.notification_logo)  // 必须设置一个图标
                .setContentText("")//正在与uni-app保持通信,保持连接中
                .setSmallIcon(R.drawable.logo)  // 必须设置一个图标
                .setPriority(NotificationCompat.PRIORITY_LOW)
//                .setOngoing(true)   // 设置通知为持续通知,不可清除
//                .setAutoCancel(false)  // ← 设置为不可自动取消
@@ -73,6 +89,7 @@
        super.onDestroy();
        isRunning = false;
    }
    public static boolean isServiceRunning() {
        return isRunning;
    }