Android:通知:Not allowed to start service Intent / Bad notification for startForeground

本文主要是介绍Android:通知:Not allowed to start service Intent / Bad notification for startForeground,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

报错一

java.lang.RuntimeException:
Unable to start receiver 包名.MainReceiver: 
java.lang.IllegalStateException: 
Not allowed to start service Intent { cmp=包名/.MainService }: 
app is in background uid UidRecord{90638 u0a10 RCVR idle change:

Android o需要适配服务

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {context.startForegroundService(mIntent);
} else {context.startService(mIntent);
}

报错二

android.app.RemoteServiceException: 
Bad notification for startForeground: 
java.lang.RuntimeException: 
invalid channel for service notification: 
Notification(channel=null pri=0 
contentView=null vibrate=null sound=null 
defaults=0x0 flags=0x40 color=0x00000000 
vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)

Android o需要适配通知channel

参考:

在Android 8.0中使用Notification中发生 Bad notification for startForeground错误

一、android8.0 添加了前台所需要的权限

添加权限如下:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

二、启动服务方式适配 :

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

三、startForeground启动通知

//service的onCreate调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {super.onStartCommand(intent, flags, startId);String CHANNEL_ID = "CHANNEL_ID";String CHANNEL_NAME = "CHANNEL_ID";NotificationChannel notificationChannel;//进行8.0的判断if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {notificationChannel = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,NotificationManager.IMPORTANCE_HIGH);notificationChannel.enableLights(true);notificationChannel.setLightColor(Color.RED);notificationChannel.setShowBadge(true);notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.createNotificationChannel(notificationChannel);}intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.jianshu.com/p/14ba95c6c3e2"));PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);Notification notification = null;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {notification = new Notification.Builder(this, CHANNEL_ID).setTicker("Nature").setSmallIcon(R.mipmap.ic_launcher).setContentTitle("这是一个测试标题").setContentIntent(pendingIntent).setContentText("这是一个测试内容").build();}notification.flags |= Notification.FLAG_NO_CLEAR;//在service里再调用startForeground方法,不然就会出现ANRstartForeground(1, notification);Log.e(TAG, "onStartCommand");return Service.START_STICKY;
}

简化:

只通知栏显示

//service的onCreate调用
private void notification() {Notification notification = null;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,NotificationManager.IMPORTANCE_LOW);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.createNotificationChannel(channel);notification = new Notification.Builder(this, CHANNEL_ID).build();}//在service里再调用startForeground方法,不然就会出现ANRstartForeground(1, notification);
}

报错 no icon :

ActivityManager: 
Attempted to start a foreground service (com.xxx.xxxx/.widget.WidgetService) with a broken notification (no icon:

经过查找源码:

  • SDK_INT< 18, postNotification()没有判断iflocalForegroundNoti.icon;
  • 18 =< SDK_INT < 23, postNotification()中if (localForegroundNoti.icon == 0);
  • 23 =< SDK_INT, postNotification()中if (localForegroundNoti.getSmallIcon() == null);

小于APi 18,传一个new Notification()是不会显示通知的。
大于等于18就不行了,但是可能通过在启动一个service,然后startForeground一个相同的id, 然后立刻stopForeground(true),可以将其消失(注意:这其实是google的bug)。
实测android 6.0生效。
但是在7.1.2上,这种方法已经失效了,所以,这些版本无法解决。
1.更好的方法是startForeground一个真实有效的notification过去。
2.startForeground只要传的id相同,不管是不是一个进程,不管是不是同一个notification,。
都会用最新的notification覆盖旧的,只显示一个。

这篇关于Android:通知:Not allowed to start service Intent / Bad notification for startForeground的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/526600

相关文章

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

嵌入式Linux驱动中的异步通知机制详解

《嵌入式Linux驱动中的异步通知机制详解》:本文主要介绍嵌入式Linux驱动中的异步通知机制,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、异步通知的核心概念1. 什么是异步通知2. 异步通知的关键组件二、异步通知的实现原理三、代码示例分析1. 设备结构

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络