溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android基礎(chǔ)(七) – Service ?;?/h1>
發(fā)布時間:2020-07-03 03:07:05 來源:網(wǎng)絡(luò) 閱讀:2194 作者:lm8751 欄目:移動開發(fā)

總結(jié):大部分應(yīng)用只需做1-2-3 步即,如業(yè)務(wù)要求比較高 可適當(dāng)采用4.

1.修改onStartCommand函數(shù)返回值為START_STICKY。(資源緊張情況下 系統(tǒng)殺掉Service后會恢復(fù),進(jìn)入恢復(fù)隊列)(建議采用)

@Override

publicintonStartCommand(Intent intent,intflags,intstartId) {

    return  START_STICKY;

   //return super.onStartCommand(intent, flags, startId);

}

 2.提升service的優(yōu)先級,設(shè)置android:priority=”1000″(建議采用)

<service

android:name=”…”

android:exported =”false”>

<!– 1000是最高優(yōu)先級,數(shù)字越小,優(yōu)先級越低 –>

<intent-filter android:priority=”1000″/>

</service>

 3.提升service進(jìn)程優(yōu)先級,為前臺進(jìn)程  (建議采用)
          步驟一:新建前臺Service (復(fù)制可用)

public class BootstrapService extends Service {
@Override public void onCreate() { super.onCreate(); startForeground(this); // 自己關(guān)閉自己 清除notification stopSelf(); }
@Override public void onDestroy() { super.onDestroy(); stopForeground(true); }
public static void startForeground(Service context) { NotificationManager nm = (NotificationManager)context. getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle(“I’m running”) .setContentText(“”) .setWhen(System.currentTimeMillis()) .setPriority(Notification.PRIORITY_MIN) .setSmallIcon(R.drawable.notification_icon) .setAutoCancel(true); Notification notification = builder.build(); context.startForeground(8888, notification); } }
步驟二:在主Service 2次調(diào)用
public class MainService extends Service {
@Override public void onCreate() { super.onCreate();
//第一次設(shè)置前臺進(jìn)程 BootstrapService.startForeground(this); //第二次設(shè)置前臺進(jìn)程 為了清除notification Intent intent = new Intent(this, BootstrapService.class); startService(intent); }
@Override public void onDestroy() { super.onDestroy(); stopForeground(true); } }
4.守護(hù)進(jìn)程(以下轉(zhuǎn),部分room 有后臺保護(hù)進(jìn)程功能)
使用Jni,在 c端 fork進(jìn)程,檢測Service是否存活,若Service已被殺死,則進(jìn)行重啟Service. 至于檢測方式,可以輪詢獲取子進(jìn)程Pid,若為1, 則說明子進(jìn)程被Init進(jìn)程所領(lǐng)養(yǎng),已經(jīng)成為了孤兒進(jìn)程. 但是這種方式比較消耗電量,并且由于不同手機(jī)系統(tǒng)定制的改變,當(dāng)應(yīng)用被強(qiáng)制停止時,父進(jìn)程并不一定被真正殺死,因此在一些特定機(jī)型上是無法通過此方式進(jìn)行判斷. 這里推薦使用liunx socket的方式進(jìn)行類似心跳包的檢測,并且當(dāng)觸發(fā)檢測Service是否被殺死之前,需要判斷應(yīng)用是否已經(jīng)被卸載,如果應(yīng)用已經(jīng)被卸載,則不再進(jìn)行檢測Service行為,直接調(diào)用exit(0)退出子進(jìn)程,避免浪費(fèi)系統(tǒng)資源和消耗電量.

   可參照:https://github.com/CharonChui/DaemonService

注意: 目前在Android 5.0系統(tǒng)上會把fork出來的進(jìn)程放到一個進(jìn)程組里, 當(dāng)程序主進(jìn)程掛掉后,也會把整個進(jìn)程組殺掉,因此用fork的方式也無法在Android5.0及以上系統(tǒng)實(shí)現(xiàn)守護(hù)進(jìn)程. 這個是系統(tǒng)層面的限制,當(dāng)然也是為了優(yōu)化整個的系統(tǒng)環(huán)境,守護(hù)進(jìn)程給手機(jī)帶來的體驗(yàn)并不好

具體見源碼:

http://androidxref.com/5.0.0_r2/xref/frameworks/base/services/core/java/com/android/server/am/Proce***ecord.java

補(bǔ):

Android5.0 以上目前已有人使用黑科技攻克,部分機(jī)型可能無法起到作用,但思路很值得借鑒,代碼結(jié)構(gòu)也不錯, 具體方案見:

https://github.com/Marswin/MarsDaemon

5.復(fù)寫Service onDestory()方法,重啟服務(wù)。(如進(jìn)程殺死無效)
6.android:persistent=“true” (需root權(quán)限才有效 ,基本無實(shí)際應(yīng)用價值)
7.通過監(jiān)聽系統(tǒng)廣播來把自己拉起來 (4.4系統(tǒng)以上 應(yīng)用退出后 將不再接受系統(tǒng)廣播 ,實(shí)際效果不明顯)

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI