相关阅读:
吊炸天!74款APP完整源码!
123个微信小程序源码分享(附下载)
[干货]2017已来,最全面试总结——这些Android面试题你一定需要
原文:http://blog.csdn.net/qq_25412055/article/details/52790980
最近公司要求要做一个提醒功能,一说到提醒,那肯定就和闹钟差不多的意思,那么肯定就要用到AlarmManager。
但是,我们知道,android系统很坑爹,不同的厂商对rom的定制,导致对进程的管理都不太相同,但是如何做到在各个手机上都能一直保持后台执行呢?。
为了解决这个问题,特地去研究了各种保持进程不被杀死的方法。
下面对几种常见的用法进行了分析,并且给出了我自己发现的一个保持进程运行的方法。
方法1:在原生的Android系统上使用AlarmManager
方法2:通过AIDL实现双进程守护机制
方法3:MarsDaemon第三方库,实现进程常驻
方法4:通过AppWiget,android桌面小组件保持进程的运行
下面是具体分析
方法1:在原生的Android系统上使用AlarmManager
“原生”这个词就对这个方法的限定很大了。我尝试了很多次,在原生的操作系统中,不需要特殊的去调用service处理。直接在某个Activity中通过AlarmManager的set和setRepeating方法设置定时后,就去杀了进程,测试结果显示,闹钟还是可以继续响的。但是这种不去特殊处理的,在第三方的rom基本都是不行的,相信大家每人敢用,因此知道就可以了
方法2:通过AIDL实现双进程守护机制
网上有很多关于AIDL实现双进程守护机制的文章,内容都是差不多,关于这种方法,都是通过在MainFest文件中指定某个Service android:process=":remote",这样就可以使这个service单开一个进程来运行。在主进程中有一个MainService,一旦RemoteService所在进程被杀死,MainService就会立刻去重新启动它,同样的,当MainService被杀死了,RemoteService就会去启动MainService,两个进程的两个Service互相监控来实现进程不销毁。
大致方法为:
1、创建一个IMyAIDLInterface.aidl文件
[java]
-
// IMyAidlInterface.aidl
-
package com.xiaoqi.alarmmanagerdemo;
-
-
interface IMyAidlInterface {
-
String getServiceName();
-
}
2、创建一个LocalService:
[java]
-
public class LocalService extends Service{
-
MyBinder binder;
-
MyConn conn;
-
@Nullable
-
@Override
-
public IBinder onBind(Intent intent) {
-
return binder;
-
}
-
-
@Override
-
public void onCreate() {
-
super.onCreate();
-
binder = new MyBinder();
-
conn = new MyConn();
-
}
-
-
class MyBinder extends IMyAidlInterface.Stub {
-
@Override
-
public String getServiceName() throws RemoteException {
-
return LocalService.class.getSimpleName();
-
}
-
}
-
-
@Override
-
public int onStartCommand(Intent intent, int flags, int startId) {
-
Toast.makeText(LocalService.this, " 本地服务活了", Toast.LENGTH_SHORT).show();
-
this.bindService(new Intent(LocalService.this,RomoteService.class),conn, Context.BIND_IMPORTANT);
-
return START_STICKY;
-
}
-
-
class MyConn implements ServiceConnection{
-
@Override
-
public void onServiceConnected(ComponentName name, IBinder service) {
-
Log.i("yangqing", "绑定上了远程服务");
-
}
-
-
@Override
-
public void onServiceDisconnected(ComponentName name) {
-
Log.i("yangqing", "远程服务被干掉了");
-
Toast.makeText(LocalService.this, "远程服务挂了", Toast.LENGTH_SHORT).show();
-
//开启远程服务
-
LocalService.this.startService(new Intent(LocalService.this,RomoteService.class));
-
//绑定远程服务
-
LocalService.this.bindService(new Intent(LocalService.this,RomoteService.class),conn,Context.BIND_IMPORTANT);
-
}
-
}
-
-
@Override
-
public void onDestroy() {
-
super.onDestroy();
-
//开启远程服务
-
LocalService.this.startService(new Intent(LocalService.this,RomoteService.class));
-
//绑定远程服务
-
LocalService.this.bindService(new Intent(LocalService.this,RomoteService.class),conn,Context.BIND_IMPORTANT);
-
-
}
-
}
3、创建一个RemoteService:
[java]
-
public class RomoteService extends Service{
-
MyConn conn;
-
MyBinder binder;
-
-
@Nullable
-
@Override
-
public IBinder onBind(Intent intent) {
-
return binder;
-
}
-
-
@Override
-
public void onCreate() {
-
super.onCreate();
-
conn = new MyConn();
-
binder = new MyBinder();
-
}
-
-
@Override
-
public int onStartCommand(Intent intent, int flags, int startId) {
-
-
Toast.makeText(this, " 远程服务活了", Toast.LENGTH_SHORT).show();
-
this.bindService(new Intent(this, LocalService.class), conn, Context.BIND_IMPORTANT);
-
-
return START_STICKY;
-
}
-
-
class MyBinder extends IMyAidlInterface.Stub {
-
@Override
-
public String getServiceName() throws RemoteException {
-
return RomoteService.class.getSimpleName();
-
}
-
}
-
-
class MyConn implements ServiceConnection {
-
-
@Override
-
public void onServiceConnected(ComponentName name, IBinder service) {
-
Log.i("yangqing", "绑定本地服务成功");
-
// Toast.makeText(RomoteService.this, "绑定本地服务成功", Toast.LENGTH_SHORT).show();
-
-
}
-
-
@Override
-
public void onServiceDisconnected(ComponentName name) {
-
Log.i("yangqing", "本地服务被干掉了");
-
Toast.makeText(RomoteService.this, "本地服务挂了", Toast.LENGTH_SHORT).show();
-
-
//开启本地服务
-
RomoteService.this.startService(new Intent(RomoteService.this, LocalService.class));
-
//绑定本地服务
-
RomoteService.this.bindService(new Intent(RomoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
-
}
-
-
}
-
-
@Override
-
public void onDestroy() {
-
super.onDestroy();
-
//开启本地服务
-
RomoteService.this.startService(new Intent(RomoteService.this, LocalService.class));
-
//绑定本地服务
-
RomoteService.this.bindService(new Intent(RomoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
-
-
}
-
}
4、在AndroidMainfest文件中注册: