服务方法android如何保证service不被杀死

最近应用开发的过程中出现了一个小问题,顺便记录一下原因和方法--服务方法

    Android开发的过程中,每次调用startService(Intent)的时候,都市调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStartCommand方法中做一些处理。

    从Android官方文档中,我们晓得onStartCommand有4种int返回值,首先简略地讲讲int返回值的作用。

    
一、onStartCommand有4种返回值:

    START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试从新创立service,由于服务状态为开始状态,所以创立服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。

    START_NOT_STICKY:“非粘性的”。应用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会主动重启该服务。

    START_REDELIVER_INTENT:重传Intent。应用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会主动重启该服务,并将Intent的值传入。

    START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。

    二、创立不被杀死的service

    1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY(或START_STICKY_COMPATIBILITY)是service被kill掉后主动重写创立

    @Override
 public int onStartCommand(Intent intent, int flags, int startId)
 {
  return START_STICKY_COMPATIBILITY; 
  //return super.onStartCommand(intent, flags, startId);
 }

    或

 @Override
 public int onStartCommand(Intent intent, int flags, int startId)
 {
  flags = START_STICKY;
  return super.onStartCommand(intent, flags, startId);
  // return START_REDELIVER_INTENT;
 }

    @Override
public void onStart(Intent intent, int startId)
{
// 再次动态注册广播
IntentFilter localIntentFilter = new IntentFilter("android.intent.action.USER_PRESENT");
localIntentFilter.setPriority(Integer.MAX_VALUE);// 整形最大值
myReceiver searchReceiver = new myReceiver();
registerReceiver(searchReceiver, localIntentFilter);

super.onStart(intent, startId);
}

    
2.在Service的onDestroy()中重启Service.

 public void onDestroy()
 {
  Intent localIntent = new Intent();
  localIntent.setClass(this, MyService.class); // 销毁时从新启动Service
  this.startService(localIntent);
 }

    3.创立一个广播

    每日一道理
今天阳光很好,坐在窗前,看窗外如此晴朗的天感觉特别舒心,雨过天晴后的世界总给人一种明媚,仿佛阳光照耀在“心田”上空,让前些天被风雨践踏的花朵从新得到爱的关怀,重现生命的活力!

    public class myReceiver extends BroadcastReceiver
{
 @Override
 public void onReceive(Context context, Intent intent)
 {
  context.startService(new Intent(context, Google.class));
 }
}

    4.AndroidManifest.xml中注册广播myReceiver及MyService服务

    <receiver android:name=".myReceiver" >
            <intent-filter android:priority="2147483647" ><!--优先级加最高-->
                <!-- 系统启动完成后会调用 -->
                <action android:name="android.intent.action.BOOT_COMPLETED" />               
                <!-- 解锁完成后会调用 -->
                <action android:name="android.intent.action.USER_PRESENT" />
                <!-- 监听情形切换 -->
                <action android:name="android.media.RINGER_MODE_CHANGED" />               
            </intent-filter>
</receiver>

    <service android:name=".MyService" >

    注:解锁,启动,切换场景激活广播需加权限,如启动完成,及手机机状态等。

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

    
 亲测ZTE U795手机Android 4.0.4版本adb push到system\app下android:persistent="true"
酿成核心程序,在360杀掉进程的时候,myReceiver还是有效,保证service重生。呃

    KILL问题:
1. settings 中stop service
onDestroy方法中,调用startService停止Service的重启。
2.settings中force stop 应用
捕捉系统停止广播(action为android.intent.action.PACKAGE_RESTARTED)
3. 借助第三方应用kill掉running task
晋升service的优先级,程序签名,或adb push到system\app下等

    相较于/data/app下的应用,放在/system/app下的应用享用更多的特权,比如若在其Manifest.xml文件中设置persistent属性为true,则可使其免受out-of-memory killer的影响。如应用程序'Phone'的AndroidManifest.xml文件:

    <application android:name="PhoneApp"

                 android:persistent="true"

                 android:label="@string/dialerIconLabel"

                 android:icon="@drawable/ic_launcher_phone">

         ...

    </application>

    设置后app晋升为系统核心级别

文章结束给大家分享下程序员的一些笑话语录: 祝大家在以后的日子里. 男生象Oracle般健壮; 女生象win7般漂亮; 桃花运象IE中毒般频繁; 钱包如Gmail容量般壮大, 升职速度赶上微软打补丁 , 追女朋友像木马一样猖獗, 生活像重装电脑后一样幸福, 写程序敲代码和聊天一样有**。

原文地址:https://www.cnblogs.com/jiangu66/p/3084584.html