編輯: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; }
類似微信朋友圈,點擊+號圖片,可以加圖片 xml: <?xml version=1.0 encoding=utf-8?> <LinearLayout
前面介紹了Android RoboGuice2 的HelloWorld示例,並介紹了從RoboGuice 1.1 升級到RoboGuice2.0 的主要注意事項。本例介紹
Android進程模型在安裝Android應用程序的時候,Android會為每個程序分配一個Linux用戶ID,並設置相應的權限,這樣其它應用程序就不能訪問此應用程序所擁
Android本質上是基於Linux內核的系統,也就是說Android就是一種Linux操作系統。只不過大多數時候都會運行在ARM架構的設備上,例如,Android手機、