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

简介:

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

 

AndroidManifest.xml:

[java]  view plain  copy
  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:

[java]  view plain  copy
  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:

[java]  view plain  copy
  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:

[java]  view plain  copy
    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.   
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5673965.html,如需转载请自行联系原作者
相关文章
|
3月前
|
Android开发 Python
Python封装ADB获取Android设备wifi地址的方法
Python封装ADB获取Android设备wifi地址的方法
61 0
|
4月前
|
XML Java Android开发
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
115 0
|
1月前
|
安全 Linux 开发者
⭐⭐⭐⭐⭐Linux C/C++ 进程崩溃诊断以及有效数据收集:解锁代码问题快速定位与修复的方法
⭐⭐⭐⭐⭐Linux C/C++ 进程崩溃诊断以及有效数据收集:解锁代码问题快速定位与修复的方法
83 1
|
6天前
|
Python
多ip多进程代理的实现方法
多ip多进程代理的实现方法
|
24天前
|
Android开发
Android调用相机与相册的方法2
Android调用相机与相册的方法
17 0
|
2月前
|
数据可视化 Android开发
[Android 四大组件] --- Service
[Android 四大组件] --- Service
24 0
|
3月前
|
Python
Python Appium Selenium 查杀进程的实用方法
Python Appium Selenium 查杀进程的实用方法
35 1
|
3月前
|
监控 应用服务中间件 nginx
Supervisor快速入门 | 使用Supervisor守护Nginx进程
Supervisor快速入门 | 使用Supervisor守护Nginx进程
43 0
|
3月前
|
安全 Java 数据安全/隐私保护
Android和iOS应用程序加固方法详解:混淆、加壳、数据加密、动态加载和数字签名实现
Android和iOS应用程序加固方法详解:混淆、加壳、数据加密、动态加载和数字签名实现
74 0
|
4月前
|
监控 安全 Java
【python实操】马上毕业了,你还不懂什么是守护线程、线程、进程?(附12306抢票程序-源代码)
【python实操】马上毕业了,你还不懂什么是守护线程、线程、进程?(附12306抢票程序-源代码)
41 0

相关实验场景

更多