从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);
}
或
flags = start_sticky;
return super.onstartcommand(intent, flags, startid);
// return start_redeliver_intent;
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
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到systemapp下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到systemapp下等
相较于/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提升为系统核心级别