保持Service不被Kill掉的方法--双Service守护,代码如下:
AndroidManifest.xml:
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <service
- android:name="ServiceOne"
- android:process=":remote" >
- <intent-filter>
- <action android:name="com.example.servicedemo.ServiceOne" />
- </intent-filter>
- </service>
- <service
- android:name="ServiceTwo"
- android:process=":remote" >
- <intent-filter>
- <action android:name="com.example.servicedemo.ServiceTwo" />
- </intent-filter>
- </service>
MainActivity.java:
- package com.example.servicedemo;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.app.ActivityManager;
- import android.app.ActivityManager.RunningServiceInfo;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Intent serviceOne = new Intent();
- serviceOne.setClass(MainActivity.this, ServiceOne.class);
- startService(serviceOne);
- Intent serviceTwo = new Intent();
- serviceTwo.setClass(MainActivity.this, ServiceTwo.class);
- startService(serviceTwo);
- }
- public static boolean isServiceWorked(Context context, String serviceName) {
- ActivityManager myManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager.getRunningServices(Integer.MAX_VALUE);
- for (int i = 0; i < runningService.size(); i++) {
- if (runningService.get(i).service.getClassName().toString().equals(serviceName)) {
- return true;
- }
- }
- return false;
- }
- }
ServiceOne.java:
- package com.example.servicedemo;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- public class ServiceOne extends Service {
- public final static String TAG = "com.example.servicedemo.ServiceOne";
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.e(TAG, "onStartCommand");
- thread.start();
- return START_STICKY;
- }
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- Timer timer = new Timer();
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- Log.e(TAG, "ServiceOne Run: "+System.currentTimeMillis());
- boolean b = MainActivity.isServiceWorked(ServiceOne.this, "com.example.servicedemo.ServiceTwo");
- if(!b) {
- Intent service = new Intent(ServiceOne.this, ServiceTwo.class);
- startService(service);
- Log.e(TAG, "Start ServiceTwo");
- }
- }
- };
- timer.schedule(task, 0, 1000);
- }
- });
- @Override
- public IBinder onBind(Intent arg0) {
- return null;
- }
- }
ServiceTwo.java:
- package com.example.servicedemo;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- public class ServiceTwo extends Service {
- public final static String TAG = "com.example.servicedemo.ServiceTwo";
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.e(TAG, "onStartCommand");
- thread.start();
- return START_REDELIVER_INTENT;
- }
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- Timer timer = new Timer();
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- Log.e(TAG, "ServiceTwo Run: " + System.currentTimeMillis());
- boolean b = MainActivity.isServiceWorked(ServiceTwo.this, "com.example.servicedemo.ServiceOne");
- if(!b) {
- Intent service = new Intent(ServiceTwo.this, ServiceOne.class);
- startService(service);
- }
- }
- };
- timer.schedule(task, 0, 1000);
- }
- });
- @Override
- public IBinder onBind(Intent arg0) {
- return null;
- }
- }