美文网首页
解决java.lang.IllegalStateExceptio

解决java.lang.IllegalStateExceptio

作者: 咖啡少年不加糖whm | 来源:发表于2019-08-02 16:25 被阅读0次

    异常描述

    java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.xxx.xxx/.service.SocketService }: app is in background uid UidRecord{1304d26 u0a289 RCVR bg:+52m38s999ms idle change:uncached procs:1 seq(0,0,0)}

        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1737)

        at android.app.ContextImpl.startService(ContextImpl.java:1693)

        at android.content.ContextWrapper.startService(ContextWrapper.java:650)

        at com.jdtz666.taojin.activity.MainActivity$a.onReceive(SourceFile:697)

        at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_59936(LoadedApk.java:1459)

        at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.$m$7(Unknown Source:4)

        at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.run(Unknown Source:39)

        at android.os.Handler.handleCallback(Handler.java:809)

        at android.os.Handler.dispatchMessage(Handler.java:102)

        at android.os.Looper.loop(Looper.java:166)

        at android.app.ActivityThread.main(ActivityThread.java:7555)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)

    通过查看后台统计报错数据,主要报错的系统版本号都是8.1.0,

    Android 8.0版本的应用不允许其创建后台服务的情况下使用startService()函数引发 IllegalStateException异常。

    新的Context. startForegroundService()函数将启动一个前台服务。即使应用在后台运行,系统也允许其调用Context. startForegroundService ()。应用必须在创建服务后五秒内调用该服务的startForeground()函数

    解决方法

    只需要在启动Service的时候判断当前版本号大于等于26,然后通过startForegroundService()方法来启动Service

    然后在Service服务的onCreate方法中加入startForeground(1, new Notication())方法即可。

    相关文章

      网友评论

          本文标题:解决java.lang.IllegalStateExceptio

          本文链接:https://www.haomeiwen.com/subject/kugldctx.html