| | |
| | | import com.hdl.photovoltaic.R; |
| | | |
| | | |
| | | |
| | | public class MyForegroundService extends Service { |
| | | private static final String CHANNEL_ID = "MyForegroundServiceChannel"; |
| | | private static final int NOTIFICATION_ID = 1; |
| | |
| | | // 创建通知渠道(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) { |
| | | // 创建通知 |
| | |
| | | super.onDestroy(); |
| | | isRunning = false; |
| | | } |
| | | |
| | | public static boolean isServiceRunning() { |
| | | return isRunning; |
| | | } |