https://github.com/fanqieVip/keeplive
集成了无声音乐(已优化功耗),前台服务、双进程守护、像素保活,jobs五种保活方式
主流的魅族、小米、锤子、vivo、努比亚、三星、华为等品牌,涵盖4.4至9.0的机型测试结果为,只要用户不主动杀死程序就不会死,但某些机型锁屏即断网的问题不是我能解决的。
使用方式,在application中启动保活服务
//定义前台服务的默认样式。即标题、描述和图标 ForegroundNotification foregroundNotification = new ForegroundNotification("测试","描述", R.mipmap.ic_launcher, //定义前台服务的通知点击事件 new ForegroundNotificationClickListener() { @Override public void foregroundNotificationClick(Context context, Intent intent) { } }); //启动保活服务 KeepLive.startWork(this, KeepLive.RunMode.ENERGY, foregroundNotification, //你需要保活的服务,如socket连接、定时任务等,建议不用匿名内部类的方式在这里写 new KeepLiveService() { /** * 运行中 * 由于服务可能会多次自动启动,该方法可能重复调用 */ @Override public void onWorking() { } /** * 服务终止 * 由于服务可能会被多次终止,该方法可能重复调用,需同onWorking配套使用,如注册和注销broadcast */ @Override public void onStop() { } } );
依赖
Maven
<dependency> <groupId>com.fanjun</groupId> <artifactId>keeplive</artifactId> <version>1.1.20</version> <type>pom</type> </dependency>
Gradle
implementation 'com.fanjun:keeplive:1.1.20'
注意事项
由于使用了双进程守护,使用该框架时一定要确保Application的onCreate中的代码在主进程使用,有些用户出现了些莫名其妙的问题,十之八九是因为这个造成的!!!