Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> android雙歷程守護

android雙歷程守護

編輯:Android開發教程

android雙進程守護

雙進程守護

 

一個應用,擁有兩個進程

RemoteService

LocalService

 

重要代碼

 

public void onCreate(){

  //這裡必須判斷,否則會出現內存溢出
  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));//啟動遠程服務
     LocalService.this.bindService(new Intent(RemoteService))  //綁定遠程服務

  }



}

 

 

 

每個Sercie中都存在Connection,

 

思路:

進程A   進程B

進程A被殺死時,啟動進程B

進程B被殺死時,啟動進程A

 

使用JobService&JobSceduler

思路:

1.獲取應用中的JobSceduler

2.通過JobSceduler來啟動LocalService和RemoteService

 

步驟:

MainActivity.java

1、啟動LocalService

2、啟動RemoteService

3、啟動JobHandlerService,它繼承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;//注意這裡

}

public boolean onStopJob(JobParameters params){

     scheduleJob(getJobInfo);
     return true;//注意這裡
}

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;
}

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved