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中的代碼在主進程使用,有些用戶出現了些莫名其妙的問題,十之八九是因爲這個造成的!!!