保持Service不被Kill掉的方法--双Service守护,代码如下:
AndroidManifest.xml:
[java]view plaincopy <activityandroid:name=".MainActivity" android:label="@string/app_name"> <intent-filter><actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter></activity><serviceandroid:name="ServiceOne" android:process=":remote"> <intent-filter><actionandroid:name="com.example.servicedemo.ServiceOne"/> </intent-filter></service><serviceandroid:name="ServiceTwo" android:process=":remote"> <intent-filter><actionandroid:name="com.example.servicedemo.ServiceTwo"/> </intent-filter></service>
MainActivity.java:
[java]view plaincopy packagecom.example.servicedemo;importjava.util.ArrayList;importandroid.app.Activity;importandroid.app.ActivityManager;importandroid.app.ActivityManager.RunningServiceInfo;importandroid.content.Context;importandroid.content.Intent;importandroid.os.Bundle;publicclassMainActivityextendsActivity{@Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);IntentserviceOne=newIntent(); serviceOne.setClass(MainActivity.this,ServiceOne.class); startService(serviceOne);IntentserviceTwo=newIntent(); serviceTwo.setClass(MainActivity.this,ServiceTwo.class); startService(serviceTwo);}publicstaticbooleanisServiceWorked(Contextcontext,StringserviceName){ ActivityManagermyManager=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);ArrayList<RunningServiceInfo>runningService=(ArrayList<RunningServiceInfo>)myManager.getRunningServices(Integer.MAX_VALUE);for(inti=0;i<runningService.size();i++){ if(runningService.get(i).service.getClassName().toString().equals(serviceName)){ returntrue; }}returnfalse; }}
ServiceOne.java:
[java]view plaincopy packagecom.example.servicedemo;importjava.util.Timer;importjava.util.TimerTask;importandroid.app.Service;importandroid.content.Intent;importandroid.os.IBinder;importandroid.util.Log;publicclassServiceOneextendsService{publicfinalstaticStringTAG="com.example.servicedemo.ServiceOne"; @Override publicintonStartCommand(Intentintent,intflags,intstartId){ Log.e(TAG,"onStartCommand"); thread.start();returnSTART_STICKY; }Threadthread=newThread(newRunnable(){ @Override publicvoidrun(){ Timertimer=newTimer(); TimerTasktask=newTimerTask(){ @Override publicvoidrun(){ Log.e(TAG,"ServiceOneRun:"+System.currentTimeMillis()); booleanb=MainActivity.isServiceWorked(ServiceOne.this,"com.example.servicedemo.ServiceTwo"); if(!b){ Intentservice=newIntent(ServiceOne.this,ServiceTwo.class); startService(service);Log.e(TAG,"StartServiceTwo"); }}};timer.schedule(task,0,1000); }});@Override publicIBinderonBind(Intentarg0){ returnnull; }}
ServiceTwo.java:
[java]view plaincopy packagecom.example.servicedemo;importjava.util.Timer;importjava.util.TimerTask;importandroid.app.Service;importandroid.content.Intent;importandroid.os.IBinder;importandroid.util.Log;publicclassServiceTwoextendsService{publicfinalstaticStringTAG="com.example.servicedemo.ServiceTwo"; @Override publicintonStartCommand(Intentintent,intflags,intstartId){ Log.e(TAG,"onStartCommand"); thread.start();returnSTART_REDELIVER_INTENT; }Threadthread=newThread(newRunnable(){ @Override publicvoidrun(){ Timertimer=newTimer(); TimerTasktask=newTimerTask(){ @Override publicvoidrun(){ Log.e(TAG,"ServiceTwoRun:"+System.currentTimeMillis()); booleanb=MainActivity.isServiceWorked(ServiceTwo.this,"com.example.servicedemo.ServiceOne"); if(!b){ Intentservice=newIntent(ServiceTwo.this,ServiceOne.class); startService(service);}}};timer.schedule(task,0,1000); }});@Override publicIBinderonBind(Intentarg0){ returnnull; }}本文转自农夫山泉别墅博客园博客,原文链接:/yaowen/p/5673965.html,如需转载请自行联系原作者