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

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

原因:

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

 

解决步骤:

 

Service.java 中

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
            startFG();
        }
    }

    private void startFG() {
        NotificationBuilder builder = new NotificationBuilder(this);
        final Notification notification = builder.buildNotification();
        startForeground(NotificationBuilder.NOTIFICATION_ID, notification);
    }
NotificationBuilder.java
public final class NotificationBuilder {
    public static final String NOTIFICATION_CHANNEL_ID = "com.demo.CHANNEL_ID";
    public static final int NOTIFICATION_ID = 0xD660;

    private final Context mContext;
    private final NotificationManager mNotificationManager;

    public NotificationBuilder(Context context) {
        mContext = context;
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public Notification buildNotification() {
        if (shouldCreateNowPlayingChannel()) {
            createNowPlayingChannel();
        }

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

        return builder.setSmallIcon(R.drawable.all_app_takeaway_icon)
                .setContentTitle("")
                .setContentTitle("")
                .setOnlyAlertOnce(true)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .build();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private boolean nowPlayingChannelExists() {
        return mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) != null;
    }

    private boolean shouldCreateNowPlayingChannel() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !nowPlayingChannelExists();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNowPlayingChannel() {
        final NotificationChannel channel = new NotificationChannel(
                NOTIFICATION_CHANNEL_ID,
                "当前播放",
                NotificationManager.IMPORTANCE_LOW);
        channel.setDescription("当前播放的电台");
        mNotificationManager.createNotificationChannel(channel);
    }
}

还要注意:

AndroidManifest.xml  中配置permission

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

启动Service方法:

        //后台启动service
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
            Intent serviceIntent = new Intent(this, DemoService.class);
            startForegroundService(serviceIntent);
        } else {
            Intent serviceIntent = new Intent(this, DemoService.class);
            startService(serviceIntent);
        }

 

 

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值