Dual process daemon
An application with two processes
RemoteService
LocalService
important code
public void onCreate(){ //This must be judged, otherwise there will be a memory overflow if(localBinder != null){ localBinder = new LocalServiceBinder(); } localServiceConnection = new LocalServiceConnection(); } public int onStartCommand(Intent intent,int flags,int startId){ startTimer(); Notification.Builder builder = new Notification.Builder(this); builder.setDefaults(NotificationCompat.DEFAULT_SOUND); builder.setContentTitle(""); builder.setSmallICon(R.mipmap....); builser.setContentInfo("info"); builder.setWhen(System.currentTimeMillis()); PendingIntent pi = PendingIntent.getActivity(this,0,intent,0); builder.setContentIntent(pi); startForeground(startId,builder,build); return START_STICKY; } class LocalServiceConnection implements ServiceConnection(){ public void onServiceConnected(ComponentName name,IBinder service){ } public void onSerivceDisConnected(CompoentName name){ LocalService.this.startService(new Intent (RemoteService));//Start the remote service LocalService.this.bindService(new Intent(RemoteService)) //Bind remote service } }
Connection exists in each Sercie,
Ideas:
Process A Process B
When process A is killed, start process B
When process B is killed, start process A
使用 JobService & JobSceduler
Ideas:
1. Get the JobSceduler in the app
2. Start LocalService and RemoteService through JobSceduler
step:
MainActivity.java
1. Start LocalService
2. Start RemoteService
3. Start JobHandlerService, which inherits Jobservice
private void scheduleJob(JobInfo job){ JobScheduler js = (JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE); js.schedule(job); } public int OnStartCommand(Intent intent,int flags,int startId){ scheduleJob(getJobInfo()); return START_NOT_START; } public JobInfo getJobInfo(){ JobInfo.Builder builder = new JobInfo.Builder(jobId,new CompoentName)(this,JobHandlerService.class); builder.setPersisted(true); builder.setPeriodic (100); builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); builder.setRequiresCharging(false); builder.setRequiresDeviceIdle(false); return builder.build(); } public boolean onStartJob(JobParameters params){ boolean isLocalServiceWorking = isServiceWork(this,"com.yourLocalservice.LocalService" boolean isLocalServiceWorking = isServiceWork(this,"com.yourLocalservice.RemoteService"); this.startService(new Intent(this,LocalService.class)); this.startService(new Intent(this,RemoteService.class)); return true;//Note here } public boolean onStopJob(JobParameters params){ scheduleJob(getJobInfo); return true;//Note here } public boolean isServiceWork(Context context,String serviceName){ ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> list = manager.getRunningService(128); if(list.size()<0){ return false; } for(int i=0;i< list.size();i++){ String name = list.get(i).service.getClassName().toString(); if(serviceName.equal(name)){ return true; } } return false; }