问题:
项目中有个锁屏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(