wjc
5 小时以前 d22974af03fb5a008cf1187e7b96cb37ea0b3093
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,8 +27,23 @@
        // 创建通知渠道(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) {
        // 创建通知
@@ -73,6 +89,7 @@
        super.onDestroy();
        isRunning = false;
    }
    public static boolean isServiceRunning() {
        return isRunning;
    }