MarsDaemon实现安卓APP进程常驻笔记

来源:互联网 发布:淘宝店全包 编辑:程序博客网 时间:2024/05/16 12:42
  项目中用到了推送功能,但是遇到了一个很头痛的问题:  程序在后台运行太久后可程序可能被强行kill或是被手机管家之类的程序给kill掉,就不能即时收到推送了。  用了N种办法,包括开启两个Service相互守护,将Service设置为前台进行,把Service的onStartCommand方法里return START_STICKY(代码如下):
     @Override    public int onStartCommand(Intent intent, int flags, int startId) {//        startForeground(1001, new Notification());//设置为前台进行        return START_STICKY;    }
 还有重写Service的onDestroy方法(这个我觉得是最没价值的,因为程序被Kill掉,反正我没监听到这个方法)。 最后我还用了Intent.ACTION_TIME_TICK(每分钟都会监听到一次)来进行广播监听等方法,结果都不理想。   百度,必应+Google也很多方法,都没有,今天看到网上有介绍MarsDaemon开源框架实现APP进程守护。   就赶紧试了下,发现效果确实比我之前试的那N种方法好得多,再赶紧把使用方法记录下来。   MarsDaemon开源地址:https://github.com/Marswin/MarsDaemon,上面也有详细的使用方法:
  • 第一步:把MarsDaemon下载解压后,把LibMarsdaemon项目作为Library项目导入到自己项目中,导入方法:File–>New–>New Module–>Import Gradle Project然后选择LibMarsdaemon即可。当然还是要在项目中Open Module Settings中进行设置才行。
  • 第二步:编写两个不同进程的Service和两个BroadCastReceiver并注册到AndroidManifest.xml中,进程名字自定义。(其中一个Service是需要常驻的进程service,需要创建一个和他同进程的BroadCastReceiver。再另外一个进程中创建一个Service和一个BroadCastReceiver。)
  • 编写自定义Application代码:
//自定义Application要继承自DaemonApplication,实现其getDaemonConfigurations方法public class GlobalApp extends DaemonApplication {    private static GlobalApp instance;    public static GlobalApp getInstance() {        return instance;    }    @Override    public void onCreate() {        super.onCreate();        instance=this;        //各种初始化    }    @Override    public void onLowMemory() {        super.onLowMemory();        System.gc();    }//在这个方法使用到我们编写的两个Service及BroadCastReceiver    @Override    protected DaemonConfigurations getDaemonConfigurations() {    //PushService是项目中需要常驻的Service    //AlarmReceiver与PushService在同一个进程        DaemonConfigurations.DaemonConfiguration configuration1 = new DaemonConfigurations.DaemonConfiguration("com.innopro.bamboo:process1", PushService.class.getCanonicalName(), AlarmReceiver.class.getCanonicalName());        DaemonConfigurations.DaemonConfiguration configuration2 = new   // MessageService和 NetworkReceiver是在统一进程的辅助类,不用实现功能。    DaemonConfigurations.DaemonConfiguration("com.innopro.bamboo:process2", MessageService.class.getCanonicalName(), NetworkReceiver.class.getCanonicalName());        //return new DaemonConfigurations(configuration1, configuration2);//listener can be null//        return new DaemonConfigurations(configuration1, configuration2);//这种方式也可以        return new DaemonConfigurations(configuration1, configuration2, new MyDaemonListener());    }    class MyDaemonListener implements DaemonConfigurations.DaemonListener {        @Override        public void onPersistentStart(Context context) {        }        @Override        public void onDaemonAssistantStart(Context context) {        }        @Override        public void onWatchDaemonDaed() {        }    }}
  • 第三步:在AndroidManifest.xml中注册Service及BroadCastReceiver,记得要添加process:
<service android:name="xx.PushService" android:process=":process1"/>  <receiver android:name="xx.AlarmReceiver" android:process=":process1"/> <service android:name="xx.MessageService"android:process=":process2" /><receiver android:name="xx.AlarmReceive  android:process=":process2"/>
MarsDaemon接入完成,现在就可以开始测试了,不过我测试 了好几款手机效果都不错,不过测试一款魅蓝Note2手机效果还是不佳。参考学习:http://blog.csdn.net/marswin89/article/details/50917098
0 1
原创粉丝点击