Not allowed to start service Intent app is in background uid UidRecord

这篇具有很好参考价值的文章主要介绍了Not allowed to start service Intent app is in background uid UidRecord。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

问题:
项目中有个锁屏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(200);
        if (info == null || info.size() == 0) return false;
        for (ActivityManager.RunningServiceInfo aInfo : info) {
            if (className.equals(aInfo.service.getClassName())) return true;
        }
        return false;
    }

    //启动service
    public static void startService(Context ctx, Class<?> cls) {
        if (!isServiceRunning(ctx, cls.getName())) {
            Intent intent = new Intent(ctx, cls);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                ctx.startForegroundService(intent);
            } else {
                ctx.startService(intent);
            }
        }
    }
    

3,开启通知,否则报错如下:


Context.startForegroundService() did not then call Service.startForeground()

意思是5秒内必须调用startForeground,可参考文章来源地址https://www.toymoban.com/news/detail-619413.html


 override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (intent != null) {            
            startServiceForeground()
        }
        return START_STICKY
    }
    
  //开启通知
  @SuppressLint("NewApi")
  private fun startServiceForeground() {
        val ns: String = Context.NOTIFICATION_SERVICE
        val nm: NotificationManager = getSystemService(ns) as NotificationManager
        var nc: NotificationChannel? = null
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            nc = NotificationChannel(
                "CoverService", "锁屏",
                NotificationManager.IMPORTANCE_LOW
            )
            nm.createNotificationChannel(nc);
            val notification = Notification.Builder(
                applicationContext, "CoverService"
            ).setContentTitle("锁屏服务中...")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.icon_logo).build()

            startForeground(2, notification)
        }
    }

 //关闭通知
 private fun stopServiceForeground() {
        stopForeground(true)
    }

 override fun onDestroy() {
        stopServiceForeground()
        super.onDestroy()
    }

到了这里,关于Not allowed to start service Intent app is in background uid UidRecord的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Failed to start mysqld.service: Unit not found.

    请仔细观看文档后,如果符合这个错误,再通过这种方式解决,不要盲目复制 问题 :Failed to start mysqld.service: Unit not found. 原因 :MySQL没有安装成功,就启动MySQL 如果是在使用\\\"yum -y install mysql-community-server\\\"命令安装mysql的时候,没有安装成功(原因:密钥已安装但是不适用此软

    2024年02月11日
    浏览(31)
  • Docker安装Failed to start docker.service: Unit docker.service not found.

    CentOS安装Docker,启动时候报错 Failed to start docker.service: Unit docker.service not found. (如下:),原因是CentOS默认使用podman代替docker,所以需要将podman卸载. 一、解决办法: 输入 yum erase podman buildah 卸载podaman,之后输入y,等待卸载直到出现“完毕”。 解决了这个问题之后便可以安装了

    2024年02月12日
    浏览(38)
  • Linux防火墙报错:Failed to start firewalld.service Unit is masked

    启动防火墙失败,报错情况如下: 原因是:(启动防火墙失败:防火墙被锁定) 解锁防火墙:(移除符号链接) Active: active (running) 看到这个已经代表启动防火墙了。 启动服务操作: 端口、ip限制:(注意:–permanent 是永久设置,没加则为临时) 问题处理:

    2024年02月12日
    浏览(35)
  • Host is not allowed to connect to this MySQL server

    问题:win10 系统上运行node,运行提示:Host is not allowed to connect to this MySQL server。 在windows10上面装完MySQL8.0.28,发现本地可以使用Navicat进行连接 ,但是在vue项目中调用node接口连接mysql却报错Host is not allowed to connect to this MySQL server 这个错误其实就是我们的MySQL不允许远程登录,所

    2024年02月13日
    浏览(33)
  • sudo service docker start显示[OK]但是* Docker is not running问题【Docker踩坑】

    首先必须添加 fstab 文件,因为由于某种原因不存在或无法读取: 由于网络控制器错误,所以要从nftables切换到iptables 执行上述命令后重新启动docker 再查看当前运行状态

    2024年02月11日
    浏览(38)
  • 连接MySQL报错,is not allowed to connect to this MySQL server

            本机装的MySQL数据库,本机可以正常连接,其他机器访问报错,is not allowed to connect to this MySQL server,防火墙等其他策略均配置没问题 。   解决方案:         出现该问题的原因是,MySQL数据库只允许自身所在的本机器连接,不允许远程连接。 1、在MySQL所在服务器

    2024年02月16日
    浏览(30)
  • Host is not allowed to connect to this MySQL server解决方法

    这个错误,其实就是我们安装的MySQL不允许远程登录,解决方法如下: 1. 在装有MySQL的机器上登录MySQL mysql -u root -p密码,执行如下命令: 该结果表示是当前的root用户限制在当前的ip内访问的,需要修改他的访问域。 2. 执行命令: 3.执行 FLUSH PRIVILEGES 或者重启 MySQL 即可;

    2024年02月04日
    浏览(40)
  • ERROR Invalid options in vue.config.js: “resolve“ is not allowed

    今天配置别名的时候不生效,怀疑是脚手架版本,查看脚手架版本,我现在的是vue cli 3.x. link rel=\\\"preload\\\" 是一种 resource hint,用来指定页面加载后很快会被用到的资源,所以在页面加载的过程中,我们希望在浏览器开始主体渲染之前尽早 preload。 默认情况下,一个 Vue CLI 应用会

    2024年02月09日
    浏览(38)
  • Host is not allowed to connect to this MySQL server的解决办法

    首先我们在学习Java时,我们链接的MySql里面没有数据,我们也不知道有些什么数据,于是我们会有一种办法来继续进行我们代码的编写。 1.第一步是我的电脑必须要和我想使用的那个人的电脑处在同一个局域网下,例如学生在学校里面可以两台电脑同时连上学校的校园网;我

    2024年02月11日
    浏览(35)
  • Navicat连接数据库出现 is not allowed to connect to this MySQL server 报错

    我们发现防火墙已经关闭了,还会出现这样的情况,那是因为mysql数据只允许自身所在的本机器连接,不允许进行远程连接 (1)在mysql服务安装的机器上进入mysql:  (2)然后使用 use mysql命令,出现如下报错。  (3)如出现上图报错,那是因为没有修改mysql的初始密码,使用

    2023年04月13日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包