说实话,这是一种流氓行为。但有些时候又是不得不需要的。比如微信的NotifyReceiver。现在抛开这些伦理的东西不讲,我们只是来看看技术上怎么实现。在后台运行的service有几个途径可以将其停止运行,第一种就是软件退出;第二种就是通过系统强制停止运行;第三种就是被某些安全软件杀死。这里我们重点关注后两种永久驻留的实现。思路有一下几个:1.通过系统的某些动作来重启Service;2.在Service的onDestory()通过一系列操作重启Service。

  先看第一种,最常见系统动作就是系统解锁动作。当系统解锁时,会发送一个广播(ACTION_USER_PRESENT)。广播接收器收到这个广播后会启动Service。具体的实现如下面代码所示:

 public class BootBroadCastReceiver extends BroadcastReceiver {

     public BootBroadCastReceiver() {
} @Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_USER_PRESENT.equals(intent.getAction())){
Log.d("PRESENT","received");
Intent nIntent=new Intent();
nIntent.setClass(context, RunningService.class);
context.startService(nIntent);
}
}
}

这里收到系统解锁的广播信息(ACTION_USER_PRESENT)后,会通过Intent的方式启动Service。Service的代码如下:

 public class RunningService extends Service {

     public RunningService() {
} @Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("start","start");
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
Intent intent=new Intent();
intent.setAction("restartService");
RunningService.this.sendBroadcast(intent);
} }

第二种方式就是在Service的OnDestory()方法中,发送一个广播信息,表示该Service将要被关闭,通过广播接收器接收到后重启Service,部分代码在上面的onDestory()中已经给出。下面给出广播接收器的代码:

 public class ServiceDestoryBroadCastReceiver extends BroadcastReceiver {

     public ServiceDestoryBroadCastReceiver() {
// TODO Auto-generated constructor stub
} @Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("restartService")){
Intent nIntent=new Intent();
nIntent.setClass(context, RunningService.class);
context.startService(nIntent);
}
} }

该广播接收器收到“restartService”的广播后,会重亲启动Service,这样便保证了Service的驻留。

这两种方法我都已经自己做过验证。尤其是第二种,当我们在系统中点击强制停止该Service后,Service能够自动重启。被安全软件杀死后是否能够重启没有做过实验。

Android实现Service永久驻留的更多相关文章

  1. Android服务(Service)研究

    Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...

  2. Android中Service 使用详解(LocalService + RemoteService)

    Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  3. android 入门-Service实时向Activity通过BroadcastReceiver传递数据

    引文: http://www.cnblogs.com/linjiqin/p/3147764.html <RelativeLayout xmlns:android="http://sch ...

  4. android 入门-Service

    sdk 1.7 package com.example.hellowrold; import java.util.Random; import com.example.hellowrold.R.id; ...

  5. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  6. Android中Service的使用

    我个人的理解是:我们平时使用的android系统的app的后台应用,就是这个原理 可以利用Service实现程序在后台运行,依照这个原理,可以通过Service来实现关键代码的运行与实现. <一 ...

  7. 【Android 】Service 全面总结

    1.Service的种类 按运行地点分类: 类别 区别  优点 缺点   应用 本地服务(Local) 该服务依附在主进程上,  服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  8. Android Activity/Service/Broadcaster三大组件之间互相调用

    我们研究两个问题,1.Service如何通过Broadcaster更改activity的一个TextView.(研究这个问题,考虑到Service从服务器端获得消息之后,将msg返回给activity ...

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

    本文分为两个部分,第一部分为双Service守护,第二部分为双进程守护 第一部分: 一.Service简介:Java.lang.Object ↳Android.content.Context  ↳an ...

随机推荐

  1. Python程序的执行原理

    1. 过程概述 Python先把代码(.py文件)编译成字节码,交给字节码虚拟机,然后虚拟机一条一条执行字节码指令,从而完成程序的执行. 2. 字节码 字节码在Python虚拟机程序里对应的是PyCo ...

  2. mysql主从复制亲测,以及注意事项

    本人亲测,windows作为mysql主服务器,linux作为从服务器,使用两个linux配置步骤都一样,测一遍而已:区别配置文件在于windwos是my.ini.linux在/etc/my.cof ...

  3. win10下nvidia控制面板看不到

    64位win10,nvidia控制面板看不到,控制面板里没有,服务里也没有nvidia相关服务,但驱动已经安装了. 解决办法: 1.下载GeForce Experience并安装 . 2.通过GeFo ...

  4. C#委托+回调详解

    今天写不完,明天会接着写的,,,, 学习C#有一段时间了,不过C#的委托+回调才这两天才会用,以前只是知道怎么用.前面的一篇文章,函数指针,其实是为这个做铺垫的,说白了委托就相当于C语言中的函数指针, ...

  5. day39

    今日内容: 1.对于表,库,记录的基本操作 2.数据库引擎的了解 3.表的详细 4.数据类型的掌握 1.回顾昨日对于表,库,记录的基本操作 库 增: create database mydb2; 删: ...

  6. 发布.net core到Centos7

    用到的软件如下 xshell,xftp,vs2017.3,centos 7.3 64位 安装环境 aliyun centos 7.3 64位 安装.net core 2.0 依赖的组件 yum ins ...

  7. OpenShift-OKD3.10基础环境部署

    单master + 双node 1.主机角色划分 #采用双网段部署 0 网段是opesnshift内部通信IP,1 网段是连接外网通信地址 #master master.example.com 192 ...

  8. Sign in with the app-specific password you generated. If you forgot the app-specific password or need to create a new one, go to appleid.apple.com

    iOS打包报错信息如下:Sign in with the app-specific password you generated. If you forgot the app-specific pas ...

  9. Trusted Cloud Summit(2018.08.14)

    时间:2018.08.14地点:北京国际会议中心

  10. .NET Core installation for Docker