zoukankan      html  css  js  c++  java
  • 保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护 2

    保持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.   
  • 相关阅读:
    Flask笔记:cookie
    Flask笔记:文件上传
    Python内置库:threading(多线程操作)
    Linux服务器架设篇,DNS服务器(三),正反解区域的配置
    Linux服务器架设篇,Windows中的虚拟机linux上不了外网怎么办?
    Linux服务器架设篇,DNS服务器(二),cache-only DNS服务器的搭建
    Linux服务器架设篇,DNS服务器(一),基础知识
    Linux网络架设篇,虚拟机l系统中网卡设备名与配置文件不符如何处理?
    Linux网络安全篇,FTP服务器的架设
    Linux基础管理篇,软件管理程序,yum与rpm
  • 原文地址:https://www.cnblogs.com/yaowen/p/5673965.html
Copyright © 2011-2022 走看看