引用:https://blog.csdn.net/qq_34195507/article/details/103634396
1.DeskService 前台服务
package com.cpsc.livedemo; import android.app.Notification; import android.app.NotificationManager; import android.app.Service; import android.content.Intent; import android.os.Build; import android.os.IBinder; import androidx.annotation.Nullable; //原文引用高版本已不可用,已修改 import android.util.Log; public class DeskService extends Service { private static final String TAG = "DaemonService"; public static final int NOTICE_ID = 100; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "DaemonService---->onCreate被调用,启动前台service"); //如果API大于18,需要弹出一个可见通知 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { Notification.Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentTitle("KeepAppAlive"); builder.setContentText("DaemonService is runing..."); startForeground(NOTICE_ID, builder.build()); // 如果觉得常驻通知栏体验不好 // 可以通过启动CancelNoticeService,将通知移除,oom_adj值不变 Intent intent = new Intent(this, CancelNoticeService.class); startService(intent); } else { startForeground(NOTICE_ID, new Notification()); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { // 如果Service被终止 // 当资源允许情况下,重启service return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); // 如果Service被杀死,干掉通知 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mManager.cancel(NOTICE_ID); } Log.d(TAG, "DaemonService---->onDestroy,前台service被杀死"); // 重启自己 Intent intent = new Intent(getApplicationContext(), DeskService.class); startService(intent); } }
2 移除前台Service通知栏标志
package com.cpsc.livedemo; import android.app.Notification; import android.app.NotificationManager; import android.app.Service; import android.content.Intent; import android.os.Build; import android.os.IBinder; import android.os.SystemClock; import androidx.annotation.Nullable; public class CancelNoticeService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) { Notification.Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); startForeground(DeskService.NOTICE_ID, builder.build()); // 开启一条线程,去移除DaemonService弹出的通知 new Thread(new Runnable() { @Override public void run() { // 延迟1s SystemClock.sleep(1000); // 取消CancelNoticeService的前台 stopForeground(true); // 移除DaemonService弹出的通知 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(DeskService.NOTICE_ID); // 任务完成,终止自己 stopSelf(); } }).start(); } return super.onStartCommand(intent, flags, startId); } }
3 注册服务到AndroidManifest.xml
<service android:name=".DeskService" android:enabled="true" android:exported="true" android:process=":daemon_service" /> <service android:name=".CancelNoticeService" android:enabled="true" android:exported="true" android:process=":service" />
4 启动服务
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_child_light_sensor); //启动服务 startService(new Intent(this, DeskService.class)); }
标签:service,保活,Intent,Build,import,android,intent,public From: https://www.cnblogs.com/cwxkurenoki/p/17345820.html