這次是前臺服務和雙進程守護兩種保活方法。
一、前臺服務部分就簡單多了,就是開啓一個前臺服務,然後在這個前臺服務內創建一個內部服務,在做相應Android API 相應的處理。具體代碼如下:
ForegroundService前臺服務裏代碼:
public class ForegroundService extends Service { private static final int SERVICE_ID = 1; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (Build.VERSION.SDK_INT < 18) { //4.3 startForeground(SERVICE_ID, new Notification()); } else if (Build.VERSION.SDK_INT < 26) { //7.0 startForeground(SERVICE_ID, new Notification()); //刪除通知欄消息 startService(new Intent(this, InnerService.class)); } else { //設置channel NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //越小,通知重要性越低 NotificationChannel channel = new NotificationChannel("channel", "dd", NotificationManager.IMPORTANCE_MIN); if (manager != null) { manager.createNotificationChannel(channel); Notification notification = new NotificationCompat.Builder(this, "channel").build(); startForeground(SERVICE_ID, notification); } } return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); // 定時打印日誌 mHandler.postDelayed(r, 100); } public static class InnerService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { startForeground(SERVICE_ID, new Notification()); stopForeground(true); stopSelf(); return super.onStartCommand(intent, flags, startId); } } Handler mHandler = new Handler(); Runnable r = new Runnable() { @Override public void run() { //do something //每隔1s循環執行run方法 KLog.e("我是前臺服務,我還活着.................."); mHandler.postDelayed(this, 1000); } }; }
清單內配置:
<service android:name=".service.ForegroundService"/> <service android:name=".service.ForegroundService$InnerService"/>
最後在MainActivity中開啓前臺服務
//前臺服務 startService(new Intent(this, ForegroundService.class));
二、雙進程守護,就是依靠一個本地服務和遠程服務,相互拉活(相互啓動)來實現始終有一個服務在運行,就算APP不在前臺,也還能夠跟後臺通訊。實現代碼也很簡單,具體如下
本地服務LocalService:
public class LocalService extends ForegroundService { @Override public int onStartCommand(Intent intent, int flags, int startId) { bindService(new Intent(LocalService.this, RemoteService.class), mConnection, Service.BIND_IMPORTANT); return super.onStartCommand(intent, flags, startId); } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { //1 } @Override public void onServiceDisconnected(ComponentName componentName) { //2 startService(new Intent(LocalService.this, RemoteService.class)); bindService(new Intent(LocalService.this, RemoteService.class), mConnection, Service.BIND_IMPORTANT); } }; }
遠程服務RemoteService:
public class RemoteService extends ForegroundService { @Override public int onStartCommand(Intent intent, int flags, int startId) { bindService(new Intent(RemoteService.this, LocalService.class), mConnection, Service.BIND_IMPORTANT); return super.onStartCommand(intent, flags, startId); } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { //1 } @Override public void onServiceDisconnected(ComponentName componentName) { //2 startService(new Intent(RemoteService.this, LocalService.class)); bindService(new Intent(RemoteService.this, LocalService.class), mConnection, Service.BIND_IMPORTANT); } }; }
然後我們需要在前臺服務ForegroundService中修改下onBind方法中創建一個LocalBinder來返回
@Nullable @Override public IBinder onBind(Intent intent) { // return null; // 前臺服務保活時使用 return new LocalBinder(); // 雙進程守護時使用 } private class LocalBinder extends Binder { }
後面還需要在清單內註冊下這兩個服務
<service android:name=".service.LocalService" /> <service android:name=".service.RemoteService" android:process=":remote" /> //注意標註
最後我們在MainActivity中啓用這兩個服務就OK了,
// 雙進程守護 startService(new Intent(this, LocalService.class)); startService(new Intent(this, RemoteService.class));
寫文章的不多,有什麼不對的和意見歡迎大家多多指教。