Android 8.0 消息推送 启动service适配

问题描述:

 

[JMessageReceiverHelper] MessageReceiver run failed:Not allowed to start service Intent { cmp=com.xxxx.xxxx/.push.NotificationService (has extras) }: app is in background uid UidRecord{e088b1e u0a276 CEM  bg:+2m43s723ms idle procs:2 seq(0,0,0)}

 

解决办法:

使用startForegroundService

在启动服务的地方:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(new Intent(context, YourService.class));
} else {
    context.startService(new Intent(context, YourService.class));
}

 

 

补充:

解决了上述问题,以为OK了,但是但是……又出现了新的问题,当APP在前台时,收到消息后崩溃了o(╥﹏╥)o,报了如下异常:

AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xxx.xxxx, PID: 8707
    android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{a2a7125 u0 com.xxx.xxxx/.push.NotificationService}

查阅大神博客得知系统创建服务后,应用有5秒的时间来调用该服务的 startForeground() 方法以显示新服务的用户可见通知。如果应用在此时间限制内未调用 startForeground(),则系统将停止服务并声明此应用为 ANR。

解决办法就是在使用了

context.startForegroundService(intent);启动服务后,在service的 onStartCommand()中调用 startFortForeground().
startForeground

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值