android o android8.0 startforegroundservice startforegroundservice() did not then call service.star

转载自:https://blog.csdn.net/legend12300/article/details/106413359

开机自启的服务出现的问题。

以下为原文:

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

原因:

  1. Android 8.0 系统不允许后台应用创建后台服务,故只能使用Context.startForegroundService()启动服务
  2. 创建服务后,应用必须在5秒内调用该服务的 startForeground() 显示一条可见通知,声明有服务在挂着,不然系统会停止服务 + ANR 套餐送上。
  3. Notification 要加 Channel,系统的要求。

解决步骤:

Service.java 中

 
  1. @Override

  2. public void onCreate() {

  3. super.onCreate();

  4. if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {

  5. startFG();

  6. }

  7. }

  8.  
  9. private void startFG() {

  10. NotificationBuilder builder = new NotificationBuilder(this);

  11. final Notification notification = builder.buildNotification();

  12. startForeground(NotificationBuilder.NOTIFICATION_ID, notification);

  13. }

NotificationBuilder.java
 
  1. public final class NotificationBuilder {

  2. public static final String NOTIFICATION_CHANNEL_ID = "com.demo.CHANNEL_ID";

  3. public static final int NOTIFICATION_ID = 0xD660;

  4.  
  5. private final Context mContext;

  6. private final NotificationManager mNotificationManager;

  7.  
  8. public NotificationBuilder(Context context) {

  9. mContext = context;

  10. mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

  11. }

  12.  
  13. public Notification buildNotification() {

  14. if (shouldCreateNowPlayingChannel()) {

  15. createNowPlayingChannel();

  16. }

  17.  
  18. NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);

  19.  
  20. return builder.setSmallIcon(R.drawable.all_app_takeaway_icon)

  21. .setContentTitle("")

  22. .setContentTitle("")

  23. .setOnlyAlertOnce(true)

  24. .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)

  25. .build();

  26. }

  27.  
  28. @RequiresApi(api = Build.VERSION_CODES.O)

  29. private boolean nowPlayingChannelExists() {

  30. return mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) != null;

  31. }

  32.  
  33. private boolean shouldCreateNowPlayingChannel() {

  34. return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !nowPlayingChannelExists();

  35. }

  36.  
  37. @RequiresApi(api = Build.VERSION_CODES.O)

  38. private void createNowPlayingChannel() {

  39. final NotificationChannel channel = new NotificationChannel(

  40. NOTIFICATION_CHANNEL_ID,

  41. "当前播放",

  42. NotificationManager.IMPORTANCE_LOW);

  43. channel.setDescription("当前播放的电台");

  44. mNotificationManager.createNotificationChannel(channel);

  45. }

  46. }

还要注意:

AndroidManifest.xml  中配置permission

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

启动Service方法:

 
  1. //后台启动service

  2. if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {

  3. Intent serviceIntent = new Intent(this, DemoService.class);

  4. startForegroundService(serviceIntent);

  5. } else {

  6. Intent serviceIntent = new Intent(this, DemoService.class);

  7. startService(serviceIntent);

  8. }

猜你喜欢

转载自blog.csdn.net/qq_24712507/article/details/108676774