Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=package/xxx.xxxService }: app is in background uid UidRecord
Android 8.0(+)不允许在后台启动/创建Service,直接调用context.startService(intent)会宕机。
临时解决办法:
将编译版本和目标版本降级到25,就可以避免因为8.0(26)不允许APP在后台启动Service而宕机。因为系统版本向后兼容。
根本解决办法:
1、SDK26 启动Service的方法:context.startForegroundService(context, Service.class);
2、SDK26 Service启动时,即在onCreate方法中,context.startForeground(id, new Notification());
2、SDK26 Service启动时,即在onCreate方法中,context.startForeground(id, new Notification());
本文介绍了Android8.0及以上版本中禁止应用程序在后台启动Service的问题,并提供了两种解决方案:一是将编译版本和目标版本降级到25;二是使用startForegroundService方法,并在Service启动时调用startForeground。
3149

被折叠的 条评论
为什么被折叠?



