Android-8.0新特性-服务启动问题

  1. 1. 问题描述
  2. 2. 原因
  3. 3. 本案例解决方式
  4. 4. 其他解决方式

问题描述

  • Android 8.0 启动后台service 出错 IllegalStateException: Not allowed to start service Intent

原因

  • 极光推送老版本sdk启动推送服务未兼容8.0才出现的问题;

Android O对应用在后台运行时可以执行的操作施加了限制,称为后台执行限制(Background Execution Limits),这可以大大减少应用的内存使用和耗电量,提高用户体验。后台执行限制分为两个部分:后台服务限制(Background Service Limitations)、广播限> 制(BroadcastLimitations)。

本案例解决方式

  • 更新极光推送sdk就好了。

其他解决方式

如果自己启动服务遇到此问题,可以这样解决

    1. 修改启动方式
1
2
3
4
5
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
    1. 并且在service里再调用startForeground方法,不然就会出现ANR
1
2
context.startForeground(SERVICE_ID, builder.getNotification());
\