解决Android8.0之后开启service时报错IllegalStateException: Not allowed to start service Intent ...
项目测试时发现的,在双击返回键关闭应用后(并未杀死后台)重新打开APP,其他手机都OK,但是8.0的手机会出现较频繁的crash。检查代码,问题锁定在重新开启应用时的startService()上。
查找资料说是Android 8.0 不再允许后台service直接通过startService方式去启动,否则就会引起IllegalStateException。而网上给出的解决方式大多是这样的:
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- context.startForegroundService(new Intent(context, MyService.class));
- } else {
- context.startService(new Intent(context, MyService.class));
- }
然后必须在Myservice中调用startForeground():
- @Override
- public void onCreate() {
- super.onCreate();
- startForeground(,new Notification());
- }
不过可能是我代码本身的问题,使用上面代码之后应用报出RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid ch...
然后做了一些修改,其他的不变,只是在要开启的service中给notification添加 channelId:
- public static final String CHANNEL_ID_STRING = "service_01";
- @Override
- public void onCreate() {
- super.onCreate();
- //适配8.0service
- NotificationManager notificationManager = (NotificationManager) MyApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
- NotificationChannel mChannel = null;
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
- mChannel = new NotificationChannel(CHANNEL_ID_STRING, getString(R.string.app_name),
- NotificationManager.IMPORTANCE_LOW);
- notificationManager.createNotificationChannel(mChannel);
- Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
- startForeground(, notification);
- }
- }
ok,可以正常跑起来了。
参考地址:https://blog.csdn.net/o279642707/article/details/82352431?utm_source=blogxgwz0
解决Android8.0之后开启service时报错IllegalStateException: Not allowed to start service Intent ...的更多相关文章
- 使用appium1.4在android8.0真机上测试程序时报错command failed shell "ps 'uiautomator'"的解决方式
appium1.4,运行自动化脚本时提示 org.openqa.selenium.SessionNotCreatedException: A new session could not be crea ...
- ThinkPHP v3.2.3 数据库读写分离,开启事务时报错:There is no active transaction
如题:ThinkPHP v3.2.3 数据库读写分离,开启事务时报错: ERR: There is no active transaction 刚开始以为是数据表引擎不对造成的,因为 有几张表的引擎是 ...
- 编译gd-2.0.35.tar.gz时报错:gd_png.c:16:53: error: png.h: No such file or directory
编译gd-2.0.35.tar.gz时报错: gcc -DHAVE_CONFIG_H -I. -I. -I. -I/usr/local/freetype/include/freetype2 -I/us ...
- windows7,windows8 64位系统 IIS7.0配置.net网站时报错:未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序。
背景: 在64位的操作系统中, IIS7.0配置.net网站时报错:未能加载文件或程序集“XXX”或它的某一个依赖项.试图加载格式不正确的程序. 解决办法: 把iis 对应的应用程序池 --高级设置- ...
- Caused by java.lang.IllegalStateException Not allowed to start service Intent { cmp=com.x.x.x/.x.x.xService }: app is in background uid UidRecord问题原因分析(二)
应用在适配Android 8.0以上系统时,会发现后台启动不了服务,会报出如下异常,并强退: Fatal Exception: java.lang.IllegalStateException Not ...
- Caused by java.lang.IllegalStateException Not allowed to start service Intent { cmp=com.x.x.x/.x.x.xService }: app is in background uid UidRecord(一)
Caused by java.lang.IllegalStateException Not allowed to start service Intent { cmp=com.x.x.x/.x.x.x ...
- 解决Android Screen Monitor在android8.0及以上系统报错:"E/Screenshot: Unsupported protocol: 2"
1.打开命令窗口,切换到 asm.jar 所在目录,执行 java -jar asm.jar,正常情况下打开后连接上设备会显示出画面 2.但是在android8.0以上系统该asm.jar包就无法正常 ...
- 解决 free(): invalid pointer: 0x00000000019ff700 运行时报错(caffe)(libtool使用)
编译成功,运行时报错: 在使用 pytorch or tensorflow or caffe 时,都可能存在这个问题: *** Error in `xxx': free(): invalid poin ...
- 使用appium在android9.0真机上测试程序时报错command failed shell “ps ‘uiautomator’”的解决办法
appium目前最新的windows版本是1.4.16,在android9.0真机上测试程序时会报错:command failed shell “ps ‘uiautomator’”. 网上大多数人的解 ...
随机推荐
- BZOJ.1016.[JSOI2008]最小生成树计数(Matrix Tree定理 Kruskal)
题目链接 最小生成树有两个性质: 1.在不同的MST中某种权值的边出现的次数是一定的. 2.在不同的MST中,连接完某种权值的边后,形成的连通块的状态是一样的. \(Solution1\) 由这两个性 ...
- BZOJ.4245.[ONTAK2015]OR-XOR(贪心)
题目链接 从高到低位贪心,判断答案的该位能否为0. 求一个前缀和sum.对于最高位,答案的这一位可以为0当且仅当至少存在m个位置满足sum[i]在这一位上为0. 注意sum[n]这一位必须为0. 如果 ...
- POJ.3145.Common Substrings(后缀数组 倍增 单调栈)
题目链接 \(Description\) 求两个字符串长度不小于k的公共子串对数. \(Solution\) 求出ht[]后先减去k,这样对于两个后缀A',B',它们之间的贡献为min{ht(A)}( ...
- Centos 7 安装 Mysql 5.5 5.6 5.7
环境 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node1 ~]# uname -a Linu ...
- 【转载】jdk1.8 LongAdder源码学习
本文转自https://blog.csdn.net/u011392897/article/details/60480108 LongAdder是jdk8新增的用于并发环境的计数器,目的是为了在高并发情 ...
- 根据请求号(request ID)查找正在运行的sql
--下面的SQL可以根据Request ID找到对应的Session信息: select * from v$session where paddr in (select addr from v$pro ...
- 如何在生产环境使用Btrace进行调试
占小狼 转载请注明原创出处,谢谢! 背景 记得前几天有人问我:在生产环境中可能经常遇到各种问题,你们一般是如何进行调试的? 很惭愧,没有经验.因为平时碰不到生产环境的服务器,定位问题需要各种数据,所以 ...
- 第十五章 dubbo结果缓存机制
dubbo提供了三种结果缓存机制: lru:基于最近最少使用原则删除多余缓存,保持最热的数据被缓存 threadlocal:当前线程缓存 jcache:可以桥接各种缓存实现 一.使用方式 <du ...
- MDX Cookbook 03 - MDX 查询中负数,零和空值 NULL 的格式化处理
FORMAT_STRING 属性在处理计算成员(通常是度量值成员)的时候会经常使用到,比如指定标准 Standard, 货币 Currency 或者 Percent 百分比格式.除此之外,还可以自定义 ...
- 国内混合APP开发技术选型
http://www.sunzhongwei.com/weex-react-native-ionic-technology-selection 选谁? 企业级应用是要考虑性能和流畅度的, 如果只是做个 ...