Android 8.0启动service报错:java.lang.RuntimeException和java.lang.IllegalStateException

错误信息:

 java.lang.RuntimeException: Unable to start receiver cn.edu.zut.broadservice.MyReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=cn.edu.zut.broadservice/.MyService }: app is in background uid UidRecord{b67c471 u0a86 RCVR idle change:uncached procs:1 seq(0,0,0)}
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3388)
at android.app.ActivityThread.access$1200(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=cn.edu.zut.broadservice/.MyService }: app is in background uid UidRecord{b67c471 u0a86 RCVR idle change:uncached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
at android.app.ContextImpl.startService(ContextImpl.java:1532)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at cn.edu.zut.broadservice.MyReceiver.onReceive(MyReceiver.java:24)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3379)
at android.app.ActivityThread.access$1200(ActivityThread.java:199) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

错误原因:

  • 如果针对 Android 8.0 的应用尝试在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException
  • 新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行,系统也允许其调用 Context.startForegroundService()。不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数。
  • 如需了解详细信息,请参阅后台执行限制

解决方案:

在 原来启动service的地方,把startService();替换为下面的代码。

Intent intent = new Intent(context, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}

并且在service的类里面加入(若加入上面代码后正常运行可忽略此内容)

@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(1,new Notification());
}

参考 :参考地址

Android studio 使用startService报错:IllegalStateException的更多相关文章

  1. android studio java工程 报错

    作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com  android studio java工程 ...

  2. android studio 9.png 报错

    Eclipse里能正常运行,但是导入到Android Studio里就报如下的错误 百度了下,说有两种解决办法一种是改后缀名,还有一种是重新在Android Studio里画一下点9图片.但是这个项目 ...

  3. 解决小米手机Android Studio安装app 报错的问题It is possible that this issue is resolved by uninstalling an existi

    问题描述 Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: Installation failed with message  ...

  4. Android 解决小米手机Android Studio安装app 报错的问题It is possible that this issue is resolved by uninstalling an existi

    Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: Installation failed with message Faile ...

  5. android studio使用部分报错处理

    1.android studio 导入项目时Error:SSL peer shut down incorrectly 今天导入一个项目到studio,显示在下载一个一个1.1.0-rc4的东西. 过了 ...

  6. Android Studio 首次安装报错 Java.lang.RuntimeException:java.lang.NullPointerException...错

    下次安装报:Java.lang.RuntimeException: java.lang.NullPointerException......错 只需在文件..\Android Studio\bin\i ...

  7. Android Studio运行SlidingView报错 FloatMath函数

    1,错误信息时这样的(图片百度的,但是提醒的是一样的) 我们点击这个错误提示,就会跳到出错的地方 2,开始的时候觉得很蛋疼,因为这个SlidingView是从别处导过来的,没什么问题把...就很久就 ...

  8. android studio view.setId报错

    自定义控件设置id的时候会报错,如:view.setId(100); 解决方法: 方案一:通过调用View.generateViewId()作为setId的参数,但此方案不是最佳方案,因为View.g ...

  9. android studio - 导入工程报错[Plugin with id 'com.android.application' not found]

    出错现象: 大概意思是找不到:com.android.application 插件,以上现象对于初学者来说会经常碰到,下面分析下产生的原因. 原因分析 首先来看看导入后的工程结构: 对于此工程结构,是 ...

随机推荐

  1. 原生js标识当前导航位置(给当前导航一个className=active)

    导航html结构为: <div class="header2-nav"> <a href="index.html">首页</a&g ...

  2. python小程序打包

    1.首先先要安装 pip install pywin32 pip install pyinstaller 没有越狱安装不了加个国内镜像地址: pip install pyinstaller -i ht ...

  3. linux将apache设置为系统服务和开机自启

    1> 查看一下/etc/init.d/下是否存在httpd这个服务 ls /etc/init.d/ | grep httpd 如果没有执行下一步 2>将自己安装目录下的apachect1复 ...

  4. VS调试SQL Server存储过程

    1.打开VS,视图-->SQL Server对象资源管理器.(我用的是VS2012) 2.添加链接,连接到数据库. 3.选择要调试的存储过程,右键,选择调试过程或者执行过程. 4.填写存储过程所 ...

  5. 克隆Rockey6加密狗复制资料

    克隆Rockey6加密狗复制资料下载 描述:Rockey6加密狗复制克隆方法Rockey6加密狗复制案例解析! 一.用OD加载DLL,并分析: 10001320 >/$ B8 4C140000 ...

  6. 基于Nginx 和 uwsgi 搭建 django.

    第一篇博客,不是很懂语法之类的,希望通过多写点东西,记录自己的成长,早点成为一个pyer. 就写下这两天折腾的这个nginx-uwsgi-django. 首先附上官方文档链接 http://uwsgi ...

  7. 前端学习历程--vue

    ---恢复内容开始--- 一.对比其他框架 1.react: 共同点: 使用 Virtual DOM 提供了响应式(Reactive)和组件化(Composable)的视图组件. 将注意力集中保持在核 ...

  8. Log4j2 简介

    介绍 Log4j2是Log4j的升级版,与之前的版本Log4j 1.x相比.有重大的改进,修正了Logback固有的架构问题的同事,改进了许多Logback所具有的功能. 特性 一.API 分离 Lo ...

  9. CSS图形

    1.正方形 最终效果:   CSS代码如下: #square { width: 100px; height: 100px; background: red; }    2.长方形 最终效果:   CS ...

  10. 2017-2018-2 『网络对抗技术』Exp1:PC平台逆向破解 20165335

    一.实验目标: 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,get ...