程序的重要性優先順序:1. 前臺程序:Foreground process使用者正在互動的Activity(onResume()當某個Service繫結正在互動的Activity被主動呼叫為前臺的Service(startForeground())元件正在執行生命週期的回撥((onCreate() /onStart()/onDestory())BroadcastReceiver 正在執行onReceive();2.可見程序:Visible process我們的Activity處在onPause() (沒有進入onStop())繫結到前臺Activity的Service3.服務程序:Service process簡單的startservice()啟動4.後臺程序:Background process對使用者沒有直接影響的程序-----Activity處於onStop()的時候5.空程序 :Empty process不含有任何的活動的元件。(android設計的,為了第二次啟動更快,採取了一個權衡)程序越往後越容易被系統殺死
如何不被系統殺死
我們要如何提升程序的優先順序(儘量做到不輕易被系統殺死),提供以下七個方案1. 模仿QQ採取在鎖屏的時候啟動1個畫素的Activity。背景:當手機鎖屏的時候什麼都乾死了,為了省電。
監聽鎖屏廣播,鎖了---啟動這個1畫素Activity。
監聽鎖屏的, 開啟---結束掉這個1畫素Activity。
要監聽鎖屏的廣播---動態註冊。
關鍵程式碼:
public class KeepLiveActivityManager { private static KeepLiveActivityManager instance; private Context context; private WeakReference<Activity> activityInstance; public static KeepLiveActivityManager getInstance(Context context) { if(instance==null){ instance = new KeepLiveActivityManager(context.getApplicationContext()); } return instance; } private KeepLiveActivityManager(Context context) { this.context = context; } public void setKeepLiveActivity(Activity activity){ activityInstance = new WeakReference<Activity>(activity); } public void startKeepLiveActivity() { Intent intent = new Intent(context, KeepLiveActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } public void finishKeepLiveActivity() { if(activityInstance!=null&&activityInstance.get()!=null){ Activity activity = activityInstance.get(); activity.finish(); } }}
原始碼地址:https://github.com/kpioneer123/KeepLiveProcess2. 大型App運營商和手機廠商可能有合作關係---白名單3. 雙程序守護一個程序被殺死,另外一個程序又被他啟動,相互監聽啟動。
A<---->B殺程序是一個一個殺的,本質是和殺程序時間賽跑。關鍵程式碼:
public class LocalService extends Service { public static final String ACTION_LOCAL_SERVICE = "com.haocai.app.keepliveprocess.LocalService"; private static final String TAG = "LocalService"; private MyServiceConnection conn; private MyBinder binder; private Intent testIntent; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); if(binder ==null){ binder = new MyBinder(); } conn = new MyServiceConnection(); } @Nullable @Override public IBinder onBind(Intent intent) { return binder; } @Override public void onDestroy() { super.onDestroy(); if(testIntent!=null){ stopService(testIntent); } //unbindService(conn); } //啟動前臺程序 增加重要性優先順序 @Override public int onStartCommand(Intent intent, int flags, int startId) { LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT); PendingIntent contentIntent = PendingIntent.getService(this, 0, intent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setTicker("360") .setContentIntent(contentIntent) .setContentTitle("我是360,我怕誰!") .setAutoCancel(true) .setContentText("hehehe") .setWhen( System.currentTimeMillis()); //把service設定為前臺執行,避免手機系統自動殺掉改服務。 startForeground(startId, builder.build()); return START_STICKY; } class MyBinder extends RemoteConnection.Stub{ @Override public String getProcessName() throws RemoteException { // TODO Auto-generated method stub return "LocalService"; } } class MyServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, "建立連線成功!"); } @Override public void onServiceDisconnected(ComponentName name) { Log.i(TAG, "本地服務被幹掉了~~~~~斷開連線!"); Toast.makeText(LocalService.this, "斷開連線", Toast.LENGTH_SHORT).show(); //啟動被幹掉的 testIntent = new Intent(); //自定義的Service的action testIntent.setAction(RemoteService.ACTION_REMOTE_SERVICE); //自定義Service的包名 testIntent.setPackage(getPackageName()); Log.i("999", getPackageName() + ""); startService(testIntent); LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT); } } }
原始碼地址:https://github.com/kpioneer123/KeepLiveProcess24. JobScheduler把任務加到系統排程佇列中,當到達任務視窗期的時候就會執行,我們可以在這個任務裡面啟動我們的程序。這樣可以做到將近殺不死的程序。
@SuppressLint("NewApi")public class JobHandleService extends JobService{ public static final String ACTION_JOB_HANDLE_SERVICE = "com.haocai.app.keepliveprocess.JobHandleService"; private int kJobId = 0; @Override public void onCreate() { super.onCreate(); Log.i("INFO", "jobService create"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("INFO", "jobService start"); scheduleJob(getJobInfo()); return START_NOT_STICKY; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public boolean onStartJob(JobParameters params) { // TODO Auto-generated method stub Log.i("INFO", "job start");// scheduleJob(getJobInfo()); boolean isLocalServiceWork = isServiceWork(this, LocalService.ACTION_LOCAL_SERVICE); boolean isRemoteServiceWork = isServiceWork (this, RemoteService.ACTION_REMOTE_SERVICE);// Log.i("INFO", "localSericeWork:"+isLocalServiceWork);// Log.i("INFO", "remoteSericeWork:"+isRemoteServiceWork); if(!isLocalServiceWork|| !isRemoteServiceWork){ //this.startService(new Intent(this,LocalService.class)); startLocalService(); startRemoteService(); //this.startService(new Intent(this,RemoteService.class)); Toast.makeText(this, "process start", Toast.LENGTH_SHORT).show(); } return true; } private void startLocalService(){ Intent testIntent = new Intent(); //自定義的Service的action testIntent.setAction(LocalService.ACTION_LOCAL_SERVICE); //自定義Service的包名 testIntent.setPackage(getPackageName()); Log.i("999",getPackageName()+""); startService(testIntent); } private void startRemoteService(){ Intent testIntent = new Intent(); //自定義的Service的action testIntent.setAction(RemoteService.ACTION_REMOTE_SERVICE); //自定義Service的包名 testIntent.setPackage(getPackageName()); Log.i("999", getPackageName() + ""); startService(testIntent); } @Override public boolean onStopJob(JobParameters params) { Log.i("INFO", "job stop");// Toast.makeText(this, "process stop", Toast.LENGTH_SHORT).show(); scheduleJob(getJobInfo()); return true; } /** Send job to the JobScheduler. */ public void scheduleJob(JobInfo t) { Log.i("INFO", "Scheduling job"); JobScheduler tm = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); tm.schedule(t); } public JobInfo getJobInfo(){ JobInfo.Builder builder = new JobInfo.Builder(kJobId++, new ComponentName (this, JobHandleService.class)); builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); builder.setPersisted(true); builder.setRequiresCharging(false); builder.setRequiresDeviceIdle(false); builder.setPeriodic(10);//間隔時間--週期 return builder.build(); } /** * 判斷某個服務是否正在執行的方法 * * @param mContext * @param serviceName * 是包名+服務的類名(例如:net.loonggg.testbackstage.TestService) * @return true代表正在執行,false代表服務沒有正在執行 */ public boolean isServiceWork(Context mContext, String serviceName) { boolean isWork = false; ActivityManager myAM = (ActivityManager) mContext .getSystemService(Context.ACTIVITY_SERVICE); List<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; }}
6. 利用賬號同步機制喚醒我們的程序AccountManager7. NDK來解決,Native程序來實現雙程序守護。
最新評論
-
1 #
-
2 #
除了白名單其他全不靈
使用這些技術的app都應該下架