码迷,mamicode.com
首页 > 移动开发 > 详细

保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护 2

时间:2016-07-15 17:11:07      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:

保持Service不被Kill掉的方法--双Service守护,代码如下:

 

AndroidManifest.xml:

  1. <activity  
  2.         android:name=".MainActivity"  
  3.         android:label="@string/app_name" >  
  4.         <intent-filter>  
  5.             <action android:name="android.intent.action.MAIN" />  
  6.             <category android:name="android.intent.category.LAUNCHER" />  
  7.         </intent-filter>  
  8.     </activity>  
  9.   
  10.     <service  
  11.         android:name="ServiceOne"  
  12.         android:process=":remote" >  
  13.         <intent-filter>  
  14.             <action android:name="com.example.servicedemo.ServiceOne" />  
  15.         </intent-filter>  
  16.     </service>  
  17.       
  18.     <service  
  19.         android:name="ServiceTwo"  
  20.         android:process=":remote" >  
  21.         <intent-filter>  
  22.             <action android:name="com.example.servicedemo.ServiceTwo" />  
  23.         </intent-filter>  
  24.     </service>  

MainActivity.java:

  1. package com.example.servicedemo;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.app.ActivityManager;  
  7. import android.app.ActivityManager.RunningServiceInfo;  
  8. import android.content.Context;  
  9. import android.content.Intent;  
  10. import android.os.Bundle;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         Intent serviceOne = new Intent();  
  20.         serviceOne.setClass(MainActivity.this, ServiceOne.class);  
  21.         startService(serviceOne);  
  22.   
  23.         Intent serviceTwo = new Intent();  
  24.         serviceTwo.setClass(MainActivity.this, ServiceTwo.class);  
  25.         startService(serviceTwo);  
  26.     }  
  27.   
  28.     public static boolean isServiceWorked(Context context, String serviceName) {  
  29.         ActivityManager myManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
  30.         ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager.getRunningServices(Integer.MAX_VALUE);  
  31.         for (int i = 0; i < runningService.size(); i++) {  
  32.             if (runningService.get(i).service.getClassName().toString().equals(serviceName)) {  
  33.                 return true;  
  34.             }  
  35.         }  
  36.         return false;  
  37.     }  
  38. }  


ServiceOne.java:

  1. package com.example.servicedemo;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import android.app.Service;  
  7. import android.content.Intent;  
  8. import android.os.IBinder;  
  9. import android.util.Log;  
  10.   
  11. public class ServiceOne extends Service {  
  12.       
  13.     public final static String TAG = "com.example.servicedemo.ServiceOne";  
  14.   
  15.     @Override  
  16.     public int onStartCommand(Intent intent, int flags, int startId) {  
  17.         Log.e(TAG, "onStartCommand");  
  18.           
  19.         thread.start();  
  20.         return START_STICKY;  
  21.     }  
  22.       
  23.     Thread thread = new Thread(new Runnable() {  
  24.           
  25.         @Override  
  26.         public void run() {  
  27.             Timer timer = new Timer();  
  28.             TimerTask task = new TimerTask() {  
  29.                   
  30.                 @Override  
  31.                 public void run() {  
  32.                     Log.e(TAG, "ServiceOne Run: "+System.currentTimeMillis());  
  33.                     boolean b = MainActivity.isServiceWorked(ServiceOne.this, "com.example.servicedemo.ServiceTwo");  
  34.                     if(!b) {  
  35.                         Intent service = new Intent(ServiceOne.this, ServiceTwo.class);  
  36.                         startService(service);  
  37.                         Log.e(TAG, "Start ServiceTwo");  
  38.                     }  
  39.                 }  
  40.             };  
  41.             timer.schedule(task, 0, 1000);  
  42.         }  
  43.     });  
  44.   
  45.     @Override  
  46.     public IBinder onBind(Intent arg0) {  
  47.         return null;  
  48.     }  
  49.   
  50. }  


ServiceTwo.java:

    1. package com.example.servicedemo;  
    2.   
    3. import java.util.Timer;  
    4. import java.util.TimerTask;  
    5.   
    6. import android.app.Service;  
    7. import android.content.Intent;  
    8. import android.os.IBinder;  
    9. import android.util.Log;  
    10.   
    11. public class ServiceTwo extends Service {  
    12.   
    13.     public final static String TAG = "com.example.servicedemo.ServiceTwo";  
    14.   
    15.     @Override  
    16.     public int onStartCommand(Intent intent, int flags, int startId) {  
    17.         Log.e(TAG, "onStartCommand");  
    18.   
    19.         thread.start();  
    20.         return START_REDELIVER_INTENT;  
    21.     }  
    22.   
    23.     Thread thread = new Thread(new Runnable() {  
    24.   
    25.         @Override  
    26.         public void run() {  
    27.             Timer timer = new Timer();  
    28.             TimerTask task = new TimerTask() {  
    29.   
    30.                 @Override  
    31.                 public void run() {  
    32.                     Log.e(TAG, "ServiceTwo Run: " + System.currentTimeMillis());  
    33.                     boolean b = MainActivity.isServiceWorked(ServiceTwo.this, "com.example.servicedemo.ServiceOne");  
    34.                     if(!b) {  
    35.                         Intent service = new Intent(ServiceTwo.this, ServiceOne.class);  
    36.                         startService(service);  
    37.                     }  
    38.                 }  
    39.             };  
    40.             timer.schedule(task, 0, 1000);  
    41.         }  
    42.     });  
    43.   
    44.     @Override  
    45.     public IBinder onBind(Intent arg0) {  
    46.         return null;  
    47.     }  
    48.   

保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护 2

标签:

原文地址:http://www.cnblogs.com/yaowen/p/5673965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!