報錯:Context.startForegroundService() did not then call Service.startForeground?

原文鏈接: https://blog.csdn.net/sinat_20059415/article/details/80584487

前言:最近在處理Android O的應用crash和anr問題,其中遇到比較多的就是“Context.startForegroundService() did not then call Service.startForeground()”,將自己的處理心得總結回顧一下。

 

demo:https://github.com/happyjiatai/demo_csdn/tree/master/demo_42_startforegroundservice

-------------------------------------------------------6月21日更新------------------------------------------------------

PS:我比較注重報錯及解決,有願意看代碼弄清代碼層次的前因後果的下面這篇寫的很好

https://blog.csdn.net/lylddinghffw/article/details/80366791

-------------------------------------------------------6月21日更新------------------------------------------------------

 

1.報錯堆棧

startForegroundService相關報錯堆棧分爲兩種:

1)

  1. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: FATAL EXCEPTION: main
  2. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: Process: packageName,,,,,,,,,,,,,,,,,,,,,,,,, PID: 3986
  3. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: java.lang.RuntimeException: java.lang.IllegalStateException: Not allowed to start service Intent { flg=0x1000000 cmp=packagename/.servicename (has extras) }: app is in background uid UidRecord{52db80 u2357s1000 TRNB bg:+2m42s199ms idle procs:3 seq(0,0,0)}
  4. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
  5. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
  6. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.os.Looper.loop(Looper.java:168)
  7. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6555)
  8. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
  9. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  10. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
  11. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { flg=0x1000000 cmp=packagename/.servicename (has extras) }: app is in background uid UidRecord{52db80 u2357s1000 TRNB bg:+2m42s199ms idle procs:3 seq(0,0,0)}
  12. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1522)
  13. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1478)
  14. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:661)
  15. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at packageName.ConnectionChangeJobService.onStartJob(ConnectionChangeJobService.java:102)
  16. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.job.JobService$1.onStartJob(JobService.java:71)
  17. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
  18. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: ... 6 more

 

2)

 

  1. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: Process: packagename, PID: 13768
  2. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
  3. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
  4. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
  5. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at android.os.Looper.loop(Looper.java:168)
  6. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6555)
  7. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
  8. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  9. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)

 

在解決問題之前先打一下基礎,看一下Android O對後臺的限制。

 

 

2.Android O 後臺執行限制

2.1 後臺執行限制

Android 8.0 爲提高電池續航時間而引入的變更之一是,當您的應用進入已緩存狀態時,如果沒有活動的組件,系統將解除應用具有的所有喚醒鎖。

此外,爲提高設備性能,系統會限制未在前臺運行的應用的某些行爲。具體而言:

現在,在後臺運行的應用對後臺服務的訪問受到限制。
應用無法使用其清單註冊大部分隱式廣播(即,並非專門針對此應用的廣播)。
默認情況下,這些限制僅適用於針對 O 的應用。不過,用戶可以從 Settings 屏幕爲任意應用啓用這些限制,即使應用並不是以 O 爲目標平臺。

Android 8.0 還對特定函數做出了以下變更:


如果針對 Android 8.0 的應用嘗試在不允許其創建後臺服務的情況下使用 startService() 函數,則該函數將引發一個 IllegalStateException。(對應於堆棧一的報錯)

 

新的 Context.startForegroundService() 函數將啓動一個前臺服務。現在,即使應用在後臺運行,系統也允許其調用 Context.startForegroundService()。不過,應用必須在創建服務後的五秒內調用該服務的 startForeground() 函數。(對應於堆棧二的報錯,需要補充)

 

 

2.2 後臺服務限制

在後臺中運行的服務會消耗設備資源,這可能降低用戶體驗。 爲了緩解這一問題,系統對這些服務施加了一些限制。

系統可以區分 前臺 和 後臺 應用。 (用於服務限制目的的後臺定義與內存管理使用的定義不同;一個應用按照內存管理的定義可能處於後臺,但按照能夠啓動服務的定義又處於前臺。)如果滿足以下任意條件,應用將被視爲處於前臺:
 

  • 具有可見 Activity(不管該 Activity 已啓動還是已暫停)。
  • 具有前臺服務。
  • 另一個前臺應用已關聯到該應用(不管是通過綁定到其中一個服務,還是通過使用其中一個內容提供程序)。 例如,如果另一個應用綁定到該應用的服務,那麼該應用處於前臺:


    IME
    壁紙服務
    通知偵聽器
    語音或文本服務
    如果以上條件均不滿足,應用將被視爲處於後臺。

綁定服務不受影響
這些規則不會對綁定服務產生任何影響。 如果您的應用定義了綁定服務,則不管應用是否處於前臺,其他組件都可以綁定到該服務。

處於前臺時,應用可以自由創建和運行前臺服務與後臺服務。 進入後臺時,在一個持續數分鐘的時間窗內,應用仍可以創建和使用服務。

在該時間窗結束後,應用將被視爲處於 空閒 狀態。 此時,系統將停止應用的後臺服務,就像應用已經調用服務的“Service.stopSelf()”方法。

在這些情況下,後臺應用將被置於一個臨時白名單中並持續數分鐘。 位於白名單中時,應用可以無限制地啓動服務,並且其後臺服務也可以運行。

處理對用戶可見的任務時,應用將被置於白名單中,例如:

處理一條高優先級 Firebase 雲消息傳遞 (FCM) 消息。

接收廣播,例如短信/彩信消息。

從通知執行 PendingIntent。

在很多情況下,您的應用都可以使用 JobScheduler 作業替換後臺服務。 例如,CoolPhotoApp 需要檢查用戶是否已經從朋友那裏收到共享的照片,即使該應用未在前臺運行。

之前,應用使用一種會檢查其雲存儲的後臺服務。 爲了遷移到 Android 8.0,開發者使用一個計劃作業替換了這種後臺服務,該作業將按一定週期啓動,查詢服務器,然後退出。

在 Android 8.0 之前,創建前臺服務的方式通常是先創建一個後臺服務,然後將該服務推到前臺。

Android 8.0 有一項複雜功能;系統不允許後臺應用創建後臺服務。 因此,Android 8.0 引入了一種全新的方法,即 Context.startForegroundService(),以在前臺啓動新服務。

在系統創建服務後,應用有五秒的時間來調用該服務的 startForeground() 方法以顯示新服務的用戶可見通知。
 

如果應用在此時間限制內未調用 startForeground(),則系統將停止服務並聲明此應用爲 ANR。

 

3. 報錯解決方案

3.1 堆棧一報錯解決方案

將 調用 startService啓動Service 改爲調用 startForegroundService,這只是第一步,後續步驟請參考堆棧二報錯解決方案。

 

  1. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: FATAL EXCEPTION: main
  2. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: Process: packageName,,,,,,,,,,,,,,,,,,,,,,,,, PID: 3986
  3. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: java.lang.RuntimeException: java.lang.IllegalStateException: Not allowed to start service Intent { flg=0x1000000 cmp=packagename/.servicename (has extras) }: app is in background uid UidRecord{52db80 u2357s1000 TRNB bg:+2m42s199ms idle procs:3 seq(0,0,0)}
  4. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
  5. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
  6. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.os.Looper.loop(Looper.java:168)
  7. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6555)
  8. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
  9. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  10. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
  11. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { flg=0x1000000 cmp=com.mediatek.providers.drm/.DrmSyncTimeService (has extras) }: app is in background uid UidRecord{52db80 u2357s1000 TRNB bg:+2m42s199ms idle procs:3 seq(0,0,0)}
  12. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1522)
  13. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1478)
  14. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:661)
  15. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at com.mediatek.providers.drm.ConnectionChangeJobService.onStartJob(ConnectionChangeJobService.java:102)
  16. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.job.JobService$1.onStartJob(JobService.java:71)
  17. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
  18. 05-28 17:49:49.693516 3986 3986 E AndroidRuntime: ... 6 more

 

3.2 堆棧二報錯解決方案

 

堆棧二簡單來看就是調用了startForegroundService後需要在Service裏繼續調用Service.startForeground()即可,但有種情況是即使調用了還是報一樣的錯。

 

  1. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: Process: packagename, PID: 13768
  2. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
  3. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
  4. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
  5. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at android.os.Looper.loop(Looper.java:168)
  6. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6555)
  7. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
  8. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  9. 06-11 15:48:15.602772 13768 13768 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)

如果認真debug,會發現Service啓動的時候不會報錯,在Service.stopSelf的時候報錯,並且catch不到異常。5s內不停止服務還會有anr問題。

原因:

看下面的代碼,有種豁然開朗學到什麼東西的感覺,但是Google把這條路封掉了,Google本意就是讓沒有可見通知的應用不可以偷偷啓動服務在後臺幹着見不得人的事,怎麼可能留下後門。

  1. Notification notification = new Notification.Builder(mContext).build();
  2. startForeground(0, notification);
  3. * @param id The identifier for this notification as per
  4. * {@link NotificationManager#notify(int, Notification)
  5. * NotificationManager.notify(int, Notification)}; must not be 0.
  6. * @param notification The Notification to be displayed.
  7. *
  8. * @see #stopForeground(boolean)
  9. */
  10. public final void startForeground(int id, Notification notification) {

結合Service的startForeground api,其中重點強調了must not be 0,即禁止是0,既然使用了0,就不要怪Google讓應用crash了。但是是在Service.stopSelf時crash,代碼分析見後文框架修改解決方案。

 

3.2.1 正統解決方案

正統解決方案肯定是Google讓怎麼做就怎麼做呀,Google讓新建一個通知那就新建一個通知。

寫了一個demo,包含正確方式(btn1)和錯誤方式(btn2)

activity:

  1. package com.example.demo_42_startforegroundservice;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.Button;
  8. public class MainActivity extends AppCompatActivity {
  9. private static final String TAG = "jiatai";
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. Button btn = findViewById(R.id.btn);
  15. Button btn2 = findViewById(R.id.btn2);
  16. btn.setOnClickListener(new View.OnClickListener() {
  17. @Override
  18. public void onClick(View v) {
  19. Log.d(TAG, "start service");
  20. Intent intent = new Intent(MainActivity.this,MyService.class);
  21. intent.putExtra("type",1);
  22. startForegroundService(intent);
  23. }
  24. });
  25. btn2.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. Log.d(TAG, "start service");
  29. Intent intent = new Intent(MainActivity.this,MyService.class);
  30. intent.putExtra("type",2);
  31. startForegroundService(intent);
  32. }
  33. });
  34. }
  35. }

Service:

  1. package com.example.demo_42_startforegroundservice;
  2. import android.app.Notification;
  3. import android.app.NotificationChannel;
  4. import android.app.NotificationManager;
  5. import android.app.Service;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.graphics.Color;
  9. import android.os.Handler;
  10. import android.os.IBinder;
  11. import android.os.Message;
  12. import android.util.Log;
  13. import android.widget.Toast;
  14. public class MyService extends Service {
  15. private static final String TAG = "jiatai";
  16. private MyHandler handler;
  17. public MyService() {
  18. }
  19. @Override
  20. public IBinder onBind(Intent intent) {
  21. // TODO: Return the communication channel to the service.
  22. throw new UnsupportedOperationException("Not yet implemented");
  23. }
  24. @Override
  25. public void onCreate() {
  26. super.onCreate();
  27. Log.d(TAG, "service oncreate");
  28. handler = new MyHandler();
  29. }
  30. @Override
  31. public int onStartCommand(Intent intent, int flags, final int startId) {
  32. int type = intent.getIntExtra("type",1);
  33. Log.d(TAG, "the create notification type is " + type + "----" + (type == 1 ? "true" : "false"));
  34. if(type == 1){
  35. createNotificationChannel();
  36. }else{
  37. createErrorNotification();
  38. }
  39. new Thread(){
  40. @Override
  41. public void run() {
  42. super.run();
  43. try {
  44. Thread.sleep(5000);
  45. } catch (InterruptedException e) {
  46. e.printStackTrace();
  47. }
  48. handler.sendEmptyMessage(startId);
  49. }
  50. }.start();
  51. return super.onStartCommand(intent, flags, startId);
  52. }
  53. private void createErrorNotification() {
  54. Notification notification = new Notification.Builder(this).build();
  55. startForeground(0, notification);
  56. }
  57. private void createNotificationChannel() {
  58. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  59. // 通知渠道的id
  60. String id = "my_channel_01";
  61. // 用戶可以看到的通知渠道的名字.
  62. CharSequence name = getString(R.string.channel_name);
  63. // 用戶可以看到的通知渠道的描述
  64. String description = getString(R.string.channel_description);
  65. int importance = NotificationManager.IMPORTANCE_HIGH;
  66. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
  67. // 配置通知渠道的屬性
  68. mChannel.setDescription(description);
  69. // 設置通知出現時的閃燈(如果 android 設備支持的話)
  70. mChannel.enableLights(true); mChannel.setLightColor(Color.RED);
  71. // 設置通知出現時的震動(如果 android 設備支持的話)
  72. mChannel.enableVibration(true);
  73. mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
  74. // 最後在notificationmanager中創建該通知渠道 //
  75. mNotificationManager.createNotificationChannel(mChannel);
  76. // 爲該通知設置一個id
  77. int notifyID = 1;
  78. // 通知渠道的id
  79. String CHANNEL_ID = "my_channel_01";
  80. // Create a notification and set the notification channel.
  81. Notification notification = new Notification.Builder(this)
  82. .setContentTitle("New Message") .setContentText("You've received new messages.")
  83. .setSmallIcon(R.drawable.ic_launcher_foreground)
  84. .setChannelId(CHANNEL_ID)
  85. .build();
  86. startForeground(1,notification);
  87. }
  88. private class MyHandler extends Handler{
  89. @Override
  90. public void handleMessage(Message msg) {
  91. super.handleMessage(msg);
  92. stopSelf(msg.what);
  93. }
  94. }
  95. @Override
  96. public void onDestroy() {
  97. super.onDestroy();
  98. Log.d(TAG, "5s onDestroy");
  99. Toast.makeText(this, "this service destroy", 1).show();
  100. stopForeground(true);
  101. }
  102. }

效果圖:

貼一下btn2 報錯堆棧:

 

  1. 06-16 09:52:12.109 783-907/system_process I/AnrManager: ANR in com.example.demo_42_startforegroundservice, time=689836
  2. Reason: Context.startForegroundService() did not then call Service.startForeground()
  3. Load: 10.13 / 9.55 / 5.83
  4. Android time :[2018-06-16 09:52:12.10] [694.364]
  1. --------- beginning of crash
  2. 06-16 09:52:12.132 7015-7015/com.example.demo_42_startforegroundservice E/AndroidRuntime: FATAL EXCEPTION: main
  3. Process: com.example.demo_42_startforegroundservice, PID: 7015
  4. android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
  5. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
  6. at android.os.Handler.dispatchMessage(Handler.java:106)
  7. at android.os.Looper.loop(Looper.java:168)
  8. at android.app.ActivityThread.main(ActivityThread.java:6555)
  9. at java.lang.reflect.Method.invoke(Native Method)
  10. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  11. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)

 

Google有點狠,crash+anr來了個全套,anr其實是我將Service延遲到了5s做完導致的,5s內如果沒有正統的調用startForeground就會anr。

 

 

PS: 網上所傳的notification隱藏是否可以?

有的需求是啓動服務,但是又不想有通知,這和Google定的規則有衝突呀,有沒有什麼辦法呢?網上2年前的方案是啓動兩個Service,一個Service幹活,另外一個Service把通知隱藏掉。

Android O Google應該考慮到這個漏洞了:

  1. private void cancelForegroundNotificationLocked(ServiceRecord r) {
  2. if (r.foregroundId != 0) {
  3. // First check to see if this app has any other active foreground services
  4. // with the same notification ID. If so, we shouldn't actually cancel it,
  5. // because that would wipe away the notification that still needs to be shown
  6. // due the other service.
  7. ServiceMap sm = getServiceMapLocked(r.userId);
  8. if (sm != null) {
  9. for (int i = sm.mServicesByName.size()-1; i >= 0; i--) {
  10. ServiceRecord other = sm.mServicesByName.valueAt(i);
  11. if (other != r && other.foregroundId == r.foregroundId
  12. && other.packageName.equals(r.packageName)) {
  13. // Found one! Abort the cancel.
  14. return;
  15. }
  16. }
  17. }
  18. r.cancelNotification();
  19. }
  20. }

如果前臺服務的通知還有被佔用,那就別想用其他服務把它幹掉了。

 

3.2.2 框架規避方案

修改方案(僅供參考):ActiveServices.java如下加一個packageName的crash的規避,anr同理,發出消息的地方可以修改爲不發出timeout消息,也可以在startForeground的時候就移除。(如果Service耗時小於5s,Service在stop流程的時候會將anr消息移除,可不修改)

  1. // Check to see if the service had been started as foreground, but being
  2. // brought down before actually showing a notification. That is not allowed.
  3. if (r.fgRequired) {
  4. Slog.w(TAG_SERVICE, "Bringing down service while still waiting for start foreground: "
  5. + r);
  6. r.fgRequired = false;
  7. r.fgWaiting = false;
  8. mAm.mHandler.removeMessages(
  9. ActivityManagerService.SERVICE_FOREGROUND_TIMEOUT_MSG, r);
  10. if (r.app != null && !"packageName".equals(r.packageName)) {
  11. Message msg = mAm.mHandler.obtainMessage(
  12. ActivityManagerService.SERVICE_FOREGROUND_CRASH_MSG);
  13. msg.obj = r.app;
  14. mAm.mHandler.sendMessage(msg);
  15. }
  16. }

 

 

 

修改原理:

編譯一個service.jar,打印報錯堆棧

  1. 01-01 07:02:17.669 918 1334 W ActivityManager: Bringing down service while still waiting for start foreground: ServiceRecord{2d44a2d u0 packageName/.servicename}
  2. 01-01 07:02:17.669 918 1334 W ActivityManager: java.lang.Throwable
  3. 01-01 07:02:17.669 918 1334 W ActivityManager: at com.android.server.am.ActiveServices.bringDownServiceLocked(ActiveServices.java:2612)
  4. 01-01 07:02:17.669 918 1334 W ActivityManager: at com.android.server.am.ActiveServices.bringDownServiceIfNeededLocked(ActiveServices.java:2559)
  5. 01-01 07:02:17.669 918 1334 W ActivityManager: at com.android.server.am.ActiveServices.stopServiceTokenLocked(ActiveServices.java:792)
  6. 01-01 07:02:17.669 918 1334 W ActivityManager: at com.android.server.am.ActivityManagerService.stopServiceToken(ActivityManagerService.java:18789)
  7. 01-01 07:02:17.669 918 1334 W ActivityManager: at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:759)
  8. 01-01 07:02:17.669 918 1334 W ActivityManager: at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3080)
  9. 01-01 07:02:17.669 918 1334 W ActivityManager: at android.os.Binder.execTransact(Binder.java:697)

找到對應拋出Context.startForegroundService() did not then call Service.startForeground()的邏輯代碼:

ActiveServices.java bringDownServiceLocked

  1. // Check to see if the service had been started as foreground, but being
  2. // brought down before actually showing a notification. That is not allowed.
  3. if (r.fgRequired) {
  4. Slog.w(TAG_SERVICE, "Bringing down service while still waiting for start foreground: "
  5. + r);
  6. r.fgRequired = false;
  7. r.fgWaiting = false;
  8. mAm.mHandler.removeMessages(
  9. ActivityManagerService.SERVICE_FOREGROUND_TIMEOUT_MSG, r);
  10. if (r.app != null) {
  11. Message msg = mAm.mHandler.obtainMessage(
  12. ActivityManagerService.SERVICE_FOREGROUND_CRASH_MSG);
  13. msg.obj = r.app;
  14. mAm.mHandler.sendMessage(msg);
  15. }
  16. }

走到這裏面繼而會由ams發出一個service_foreground_crash_msg的消息,導致crash。

至於爲嘛會走到這裏呢,都是id = 0 的過,既沒有走前臺服務的流程也沒有將r.fgRequired設爲false,anr的msg也沒有移除掉。

  1. private void setServiceForegroundInnerLocked(ServiceRecord r, int id,
  2. Notification notification, int flags) {
  3. if (id != 0) {
  4. if (notification == null) {
  5. throw new IllegalArgumentException("null notification");
  6. }
  7. // Instant apps need permission to create foreground services.
  8. ...
  9. if (r.fgRequired) {
  10. if (DEBUG_SERVICE || DEBUG_BACKGROUND_CHECK) {
  11. Slog.i(TAG, "Service called startForeground() as required: " + r);
  12. }
  13. r.fgRequired = false;
  14. r.fgWaiting = false;
  15. mAm.mHandler.removeMessages(
  16. ActivityManagerService.SERVICE_FOREGROUND_TIMEOUT_MSG, r);
  17. }
  18. if (r.foregroundId != id) {
  19. cancelForegroundNotificationLocked(r);
  20. r.foregroundId = id;
  21. }
  22. notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
  23. r.foregroundNoti = notification;
  24. if (!r.isForeground) {
  25. final ServiceMap smap = getServiceMapLocked(r.userId);
  26. if (smap != null) {
  27. ActiveForegroundApp active = smap.mActiveForegroundApps.get(r.packageName);
  28. if (active == null) {
  29. active = new ActiveForegroundApp();
  30. active.mPackageName = r.packageName;
  31. active.mUid = r.appInfo.uid;
  32. active.mShownWhileScreenOn = mScreenOn;
  33. if (r.app != null) {
  34. active.mAppOnTop = active.mShownWhileTop =
  35. r.app.uidRecord.curProcState
  36. <= ActivityManager.PROCESS_STATE_TOP;
  37. }
  38. active.mStartTime = active.mStartVisibleTime
  39. = SystemClock.elapsedRealtime();
  40. smap.mActiveForegroundApps.put(r.packageName, active);
  41. requestUpdateActiveForegroundAppsLocked(smap, 0);
  42. }
  43. active.mNumActive++;
  44. }
  45. r.isForeground = true;
  46. }
  47. r.postNotification();
  48. if (r.app != null) {
  49. updateServiceForegroundLocked(r.app, true);
  50. }
  51. getServiceMapLocked(r.userId).ensureNotStartingBackgroundLocked(r);
  52. mAm.notifyPackageUse(r.serviceInfo.packageName,
  53. PackageManager.NOTIFY_PACKAGE_USE_FOREGROUND_SERVICE);
  54. } else {
  55. if (r.isForeground) {
  56. final ServiceMap smap = getServiceMapLocked(r.userId);
  57. if (smap != null) {
  58. decActiveForegroundAppLocked(smap, r);
  59. }
  60. r.isForeground = false;
  61. if (r.app != null) {
  62. mAm.updateLruProcessLocked(r.app, false, null);
  63. updateServiceForegroundLocked(r.app, true);
  64. }
  65. }
  66. if ((flags & Service.STOP_FOREGROUND_REMOVE) != 0) {
  67. cancelForegroundNotificationLocked(r);
  68. r.foregroundId = 0;
  69. r.foregroundNoti = null;
  70. } else if (r.appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
  71. r.stripForegroundServiceFlagFromNotification();
  72. if ((flags & Service.STOP_FOREGROUND_DETACH) != 0) {
  73. r.foregroundId = 0;
  74. r.foregroundNoti = null;
  75. }
  76. }
  77. }
  78. }

anr的時限爲嘛是5s呢?

  1. void scheduleServiceForegroundTransitionTimeoutLocked(ServiceRecord r) {
  2. if (r.app.executingServices.size() == 0 || r.app.thread == null) {
  3. return;
  4. }
  5. Message msg = mAm.mHandler.obtainMessage(
  6. ActivityManagerService.SERVICE_FOREGROUND_TIMEOUT_MSG);
  7. msg.obj = r;
  8. r.fgWaiting = true;
  9. mAm.mHandler.sendMessageDelayed(msg, SERVICE_START_FOREGROUND_TIMEOUT);
  10. }
  11. // How long the startForegroundService() grace period is to get around to
  12. // calling startForeground() before we ANR + stop it.
  13. static final int SERVICE_START_FOREGROUND_TIMEOUT = 5*1000;

這種timeout流程就很熟悉了。

 

4. 總結

Android O 後臺應用想啓動服務就老老實實的加個notification給用戶看,表示你自己在後臺佔着資源,殺不殺由用戶決定,偷偷地在後臺跑沒有framework幫忙想都別想,一個anr+crash套餐瞭解一下。

1)activity: Context.startForegroundService()

2)Service:startForeground(int id, Notification notification)(id must not be 0)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章