之前,低版本做守護進程時,就是兩個service 相互守護,但是大於5.0後,似乎就不管用了,今天我們來看看,怎麼解決這個問題,多的不說,直接上代碼:
首先是有業務處理的service:
public class GuardMessageService extends Service{
@Override
public void onCreate() {
super.onCreate();
new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println("=====守護進程======");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(1,new Notification());
bindService(new Intent(this, GuardService.class),connection, Context.BIND_IMPORTANT);
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new GuardAidl.Stub() {};
}
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//建立連接
Toast.makeText(GuardMessageService.this,"建立連接",Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
//連接斷開了
startService(new Intent(GuardMessageService.this,GuardService.class));
bindService(new Intent(GuardMessageService.this, GuardService.class),connection, Context.BIND_IMPORTANT);
}
};
}
2.守護的service:
public class GuardService extends Service{
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new GuardAidl.Stub() {};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(1,new Notification());
bindService(new Intent(this, GuardMessageService.class),connection, Context.BIND_IMPORTANT);
return START_STICKY;
}
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//建立連接
Toast.makeText(GuardService.this,"建立連接",Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
//連接斷開了
startService(new Intent(GuardService.this,GuardMessageService.class));
bindService(new Intent(GuardService.this, GuardMessageService.class),connection, Context.BIND_IMPORTANT);
}
};
}
3.爲了解決5.0 以上,設置的一個service:
public class JobWakeUpService extends JobService{
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
JobInfo.Builder jobBuilder = new JobInfo.Builder(
1,new ComponentName(this,JobWakeUpService.class));
jobBuilder.setPeriodic(2000);
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(jobBuilder.build());
return START_STICKY;
}
@Override
public boolean onStartJob(JobParameters params) {
// 判斷服務有沒有在運行
boolean messageServiceAlive = serviceAlive(GuardMessageService.class.getName());
if(!messageServiceAlive){
startService(new Intent(this,GuardMessageService.class));
}
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
/**
* 判斷某個服務是否正在運行的方法
*/
private boolean serviceAlive(String serviceName) {
boolean isWork = false;
ActivityManager myAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(100);
if (myList.size() <= 0) {
return false;
}
for (int i = 0; i < myList.size(); i++) {
String mName = myList.get(i).service.getClassName().toString();
if (mName.equals(serviceName)) {
isWork = true;
break;
}
}
return isWork;
}
}
最後是AndroidManifest.xml的配置:
// JobService權限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".services.GuardService"
android:process=":guardService"/>
<service android:name=".services.GuardMessageService"/>
<service android:name=".services.JobWakeUpService"
android:enabled="true"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
調用:
//啓動守護進程
startService(new Intent(mContext, GuardMessageService.class));
startService(new Intent(mContext, GuardService.class));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//必須大於5.0
startService(new Intent(mContext,JobWakeUpService.class));
}