问题:
项目中有个锁屏service,间隔30秒屏幕黑屏,向右滑动解锁,防止误操作的,最近客户反馈崩溃次数有点多
查看崩溃日志如下
java.lang.IllegalStateException
Not allowed to start service Intent {
xxx.xxxService }: app is in background uid UidRecord{
91b7553 u0a119 CAC bg:+21m20s281ms idle procs:2 seq(0,0,0)}
xxx(xxx:124)
分析:
android 8.0(O)以后后台服务做了限制
解决:
1,在manifests.xml加入权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
2,启动服务startService改动
public static boolean isServiceRunning(Context ctx, final String className) {
ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> info = am.getRunningServices(

文章描述了一个在Android8.0及以上版本遇到的锁屏服务崩溃问题,原因是后台服务限制。解决方案包括在manifest添加FOREGROUND_SERVICE权限,检查服务是否已运行,以及在启动服务时使用startForegroundService,并在5秒内调用startForeground来显示通知。此外,还提供了创建和关闭通知通道的方法。
最低0.47元/天 解锁文章
4109

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



