如何保证service不被杀掉
2016-04-14 17:17
295 查看
1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
在setting里kill掉,没能重启
2.在Service的onDestroy()中重启Service.
public void onDestroy() {
Intent localIntent = new Intent();
localIntent.setClass(this, MyService.class); //销毁时重新启动Service
this.startService(localIntent);
}
通过别的应用,直接kill掉我的应用时,是不会调用这个方法的
但是在settings 中stop service,onDestroy方法中,调用startService进行Service的重启。
3.settings中force stop 应用
捕捉系统进行广播(action为android.intent.action.PACKAGE_RESTARTED)
[java] view
plain copy
public class ProtectorHelperReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String pkg = "com.innofidei.myprotector";// 被kill的应用的包名
String action = pkg + ".action.START_SERVICE";// 重启service的acition
String str = intent.getData().toString().toLowerCase().replace("package:", "");
String data = intent.getAction();
if (str != null && str.equals(pkg)) {
if (data != null && data.equals("android.intent.action.PACKAGE_REMOVED")) {
File file = new File("/sdcard/" + context.getPackageName() + "/ProtectorHelper.apk");
file.delete();
file.getParentFile().delete();
Intent intent2 = new Intent(context, UninstallActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
} else {
context.sendBroadcast(new Intent(action));// 通知应用重启service
}
}
}
}
4. 借助第三方应用kill掉running task
提升service的优先级
setForeground(true) ;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
在setting里kill掉,没能重启
2.在Service的onDestroy()中重启Service.
public void onDestroy() {
Intent localIntent = new Intent();
localIntent.setClass(this, MyService.class); //销毁时重新启动Service
this.startService(localIntent);
}
通过别的应用,直接kill掉我的应用时,是不会调用这个方法的
但是在settings 中stop service,onDestroy方法中,调用startService进行Service的重启。
3.settings中force stop 应用
捕捉系统进行广播(action为android.intent.action.PACKAGE_RESTARTED)
[java] view
plain copy
public class ProtectorHelperReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String pkg = "com.innofidei.myprotector";// 被kill的应用的包名
String action = pkg + ".action.START_SERVICE";// 重启service的acition
String str = intent.getData().toString().toLowerCase().replace("package:", "");
String data = intent.getAction();
if (str != null && str.equals(pkg)) {
if (data != null && data.equals("android.intent.action.PACKAGE_REMOVED")) {
File file = new File("/sdcard/" + context.getPackageName() + "/ProtectorHelper.apk");
file.delete();
file.getParentFile().delete();
Intent intent2 = new Intent(context, UninstallActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
} else {
context.sendBroadcast(new Intent(action));// 通知应用重启service
}
}
}
}
4. 借助第三方应用kill掉running task
提升service的优先级
setForeground(true) ;
相关文章推荐
- JavaMail实现邮件发送工具类
- 软考中高项学员:2016年4月13日作业
- afnetworing 上传json 格式的数据
- TCP/IP详解学习笔记(13)-TCP坚持定时器,TCP保活定时器
- 简单的js打印机效果
- 利用小闹钟计算1~100求和
- 9个循环2
- RandomForest随机森林感想
- OA项目实战学习(6)——权限模型设计
- java集合框架07——Map架构与源码分析
- Displaying Graphics with OpenGL ES(一)——构建一个OpenGL ES环境
- TCP/IP详解学习笔记(12)-TCP的超时与重传
- 批量往数据库导入数据遇到的问题总结
- 比较两份文件的异同-beyond compare
- VB作业
- Tableview快速上手(方法注释到位哦)
- 太原网络营销师百度竞价(SEM)账户层级设置如何设置?
- thinkpython2
- 求和
- 易点固定资产管理云平台: “互联网+”下的固定资产管理设想