当前位置:   article > 正文

Context.startForegroundService() did not then call Service.startForeground()

context.startforegroundservice() did not then call service.startforeground()

从 Android 中service 详解 和Android service 启动篇之 startForegroundService 中我们知道在android 8.0 禁止启动后台服务。提出通过startForegroundService() 启动前台服务。但是必须要配合在service 中调用Service.startForeground(),不然就会出现ANR 或者crash。

ANR log 如下:

  1. 11-06 02:01:59.616 3877 3893 E ActivityManager: ANR in com.shift.phonemanager.permission.accesslog
  2. 11-06 02:01:59.616 3877 3893 E ActivityManager: PID: 1369
  3. 11-06 02:01:59.616 3877 3893 E ActivityManager: Reason: Context.startForegroundService() did not then call Service.startForeground()
  4. 11-06 02:01:59.616 3877 3893 E ActivityManager: Load: 0.0 / 0.0 / 0.0
  5. 11-06 02:01:59.616 3877 3893 E ActivityManager: CPU usage from 7945ms to 0ms ago (2007-11-06 02:01:51.418 to 2007-11-06 02:01:59.363):
  6. 11-06 02:01:59.616 3877 3893 E ActivityManager: 60% 3877/system_server: 35% user + 25% kernel / faults: 3744 minor 6 major
  7. 11-06 02:01:59.616 3877 3893 E ActivityManager: 25% 1042/com.android.launcher3: 20% user + 4.9% kernel / faults: 11190 minor 9 major
  8. 11-06 02:01:59.616 3877 3893 E ActivityManager: 18% 1149/android.process.media: 13% user + 5.3% kernel / faults: 6130 minor
  9. 11-06 02:01:59.616 3877 3893 E ActivityManager: 15% 1420/adbd: 3.6% user + 11% kernel / faults: 5074 minor
  10. 11-06 02:01:59.616 3877 3893 E ActivityManager: 9.7% 255/logd: 2.7% user + 6.9% kernel / faults: 5 minor
  11. 11-06 02:01:59.616 3877 3893 E ActivityManager: 9.2% 3814/surfaceflinger: 4.4% user + 4.8% kernel / faults: 658 minor

 

crash log 如下:

  1. --------- beginning of crash
  2. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: FATAL EXCEPTION: main
  3. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: Process: com.shift.phonemanager.permission.accesslog, PID: 3106
  4. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
  5. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
  6. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
  7. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
  8. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6518)
  9. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
  10. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  11. 11-06 02:06:05.307 3106 3106 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  12. 11-06 02:06:05.320 3118 3118 D ExtensionsFactory: No custom extensions.

 

实例:

  1. private void showNotification() {
  2. // In this sample, we'll use the same text for the ticker and the expanded notification
  3. CharSequence text = getText(R.string.local_service_started);
  4. // The PendingIntent to launch our activity if the user selects this notification
  5. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
  6. new Intent(this, LocalServiceActivities.Controller.class), 0);
  7. // Set the info for the views that show in the notification panel.
  8. Notification notification = new Notification.Builder(this)
  9. .setSmallIcon(R.drawable.stat_sample) // the status icon
  10. .setTicker(text) // the status text
  11. .setWhen(System.currentTimeMillis()) // the time stamp
  12. .setContentTitle(getText(R.string.local_service_label)) // the label of the entry
  13. .setContentText(text) // the contents of the entry
  14. .setContentIntent(contentIntent) // The intent to send when the entry is clicked
  15. .build();
  16. // Send the notification.
  17. mNM.notify(NOTIFICATION, notification);
  18. }

 

实例:

  1. private void setForegroundNotification() {
  2. NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  3. String channelId = "access_log_channel";
  4. NotificationChannel channel = new NotificationChannel(channelId, "channel_1",
  5. NotificationManager.IMPORTANCE_DEFAULT);
  6. nm.createNotificationChannel(channel);
  7. Notification notification = new Notification.Builder(this).setChannelId(channelId)
  8. .setSmallIcon(R.drawable.ic_launcher)
  9. .setContentTitle(getString(R.string.permission_access_log_title))
  10. .build();
  11. startForeground(1, notification);
  12. }

 

 

参考:

https://developer.android.com/about/versions/oreo/background

https://developer.android.com/reference/android/app/Service

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号