专栏名称: 开发者全社区
分享和推送Java/Android方向的技术和文章,让你成为这方面的大牛,让你每天都成长一点。同时,我们也会邀请BAT的大牛分享原创!
目录
相关文章推荐
开发者全社区  ·  字节CEO全员会反思DeepSeek影响 ·  昨天  
开发者全社区  ·  43岁冰冰显老了 ·  昨天  
开发者全社区  ·  央企入职半年发5W多年终奖 ·  2 天前  
开发者全社区  ·  突发:DeepSeek被封杀 ·  2 天前  
开发者全社区  ·  麻了!阿里P7炫总包超150w ·  2 天前  
51好读  ›  专栏  ›  开发者全社区

如何让你的App永远在后台存活:对Android进程守护、闹钟后台被杀死的研究

开发者全社区  · 公众号  · android  · 2017-01-14 11:41

正文

相关阅读:

吊炸天!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]

  1. // IMyAidlInterface.aidl

  2. package com.xiaoqi.alarmmanagerdemo;

  3. interface IMyAidlInterface {

  4. String getServiceName();

  5. }


2、创建一个LocalService:

[java]

  1. public class LocalService extends Service{

  2. MyBinder binder;

  3. MyConn conn;

  4. @Nullable

  5. @Override

  6. public IBinder onBind(Intent intent) {

  7. return binder;

  8. }

  9. @Override

  10. public void onCreate() {

  11. super.onCreate();

  12. binder = new MyBinder();

  13. conn = new MyConn();

  14. }

  15. class MyBinder extends IMyAidlInterface.Stub {

  16. @Override

  17. public String getServiceName() throws RemoteException {

  18. return LocalService.class.getSimpleName();

  19. }

  20. }

  21. @Override

  22. public int onStartCommand(Intent intent, int flags, int startId) {

  23. Toast.makeText(LocalService.this, " 本地服务活了", Toast.LENGTH_SHORT).show();

  24. this.bindService(new Intent(LocalService.this,RomoteService.class),conn, Context.BIND_IMPORTANT);

  25. return START_STICKY;

  26. }

  27. class MyConn implements ServiceConnection{

  28. @Override

  29. public void onServiceConnected(ComponentName name, IBinder service) {

  30. Log.i("yangqing", "绑定上了远程服务");

  31. }

  32. @Override

  33. public void onServiceDisconnected(ComponentName name) {

  34. Log.i("yangqing", "远程服务被干掉了");

  35. Toast.makeText(LocalService.this, "远程服务挂了", Toast.LENGTH_SHORT).show();

  36. //开启远程服务

  37. LocalService.this.startService(new Intent(LocalService.this,RomoteService.class));

  38. //绑定远程服务

  39. LocalService.this.bindService(new Intent(LocalService.this,RomoteService.class),conn,Context.BIND_IMPORTANT);

  40. }

  41. }

  42. @Override

  43. public void onDestroy() {

  44. super.onDestroy();

  45. //开启远程服务

  46. LocalService.this.startService(new Intent(LocalService.this,RomoteService.class));

  47. //绑定远程服务

  48. LocalService.this.bindService(new Intent(LocalService.this,RomoteService.class),conn,Context.BIND_IMPORTANT);

  49. }

  50. }


3、创建一个RemoteService:

[java]

  1. public class RomoteService extends Service{

  2. MyConn conn;

  3. MyBinder binder;

  4. @Nullable

  5. @Override

  6. public IBinder onBind(Intent intent) {

  7. return binder;

  8. }

  9. @Override

  10. public void onCreate() {

  11. super.onCreate();

  12. conn = new MyConn();

  13. binder = new MyBinder();

  14. }

  15. @Override

  16. public int onStartCommand(Intent intent, int flags, int startId) {

  17. Toast.makeText(this, " 远程服务活了", Toast.LENGTH_SHORT).show();

  18. this.bindService(new Intent(this, LocalService.class), conn, Context.BIND_IMPORTANT);

  19. return START_STICKY;

  20. }

  21. class MyBinder extends IMyAidlInterface.Stub {

  22. @Override

  23. public String getServiceName() throws RemoteException {

  24. return RomoteService.class.getSimpleName();

  25. }

  26. }

  27. class MyConn implements ServiceConnection {

  28. @Override

  29. public void onServiceConnected(ComponentName name, IBinder service) {

  30. Log.i("yangqing", "绑定本地服务成功");

  31. // Toast.makeText(RomoteService.this, "绑定本地服务成功", Toast.LENGTH_SHORT).show();

  32. }

  33. @Override

  34. public void onServiceDisconnected(ComponentName name) {

  35. Log.i("yangqing", "本地服务被干掉了");

  36. Toast.makeText(RomoteService.this, "本地服务挂了", Toast.LENGTH_SHORT).show();

  37. //开启本地服务

  38. RomoteService.this.startService(new Intent(RomoteService.this, LocalService.class));

  39. //绑定本地服务

  40. RomoteService.this.bindService(new Intent(RomoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);

  41. }

  42. }

  43. @Override

  44. public void onDestroy() {

  45. super.onDestroy();

  46. //开启本地服务

  47. RomoteService.this.startService(new Intent(RomoteService.this, LocalService.class));

  48. //绑定本地服务

  49. RomoteService.this.bindService(new Intent(RomoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);

  50. }

  51. }


4、在AndroidMainfest文件中注册:







请到「今天看啥」查看全文


推荐文章
开发者全社区  ·  字节CEO全员会反思DeepSeek影响
昨天
开发者全社区  ·  43岁冰冰显老了
昨天
开发者全社区  ·  央企入职半年发5W多年终奖
2 天前
开发者全社区  ·  突发:DeepSeek被封杀
2 天前
开发者全社区  ·  麻了!阿里P7炫总包超150w
2 天前
企鹅吃喝指南  ·  大热天,长相思才是最被渴望的小鲜肉!
7 年前