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 DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

如何关闭Mac的Safari通知? 3招教你关闭Safari浏览器网站通知的技巧

《如何关闭Mac的Safari通知?3招教你关闭Safari浏览器网站通知的技巧》当我们在使用Mac电脑专注做一件事情的时候,总是会被一些消息推送通知所打扰,这时候,我们就希望关闭这些烦人的Mac通... Safari 浏览器的「通知」功能本意是为了方便用户及时获取最新资讯,但很容易被一些网站滥用,导致我们

Java 的 Condition 接口与等待通知机制详解

《Java的Condition接口与等待通知机制详解》在Java并发编程里,实现线程间的协作与同步是极为关键的任务,本文将深入探究Condition接口及其背后的等待通知机制,感兴趣的朋友一起看... 目录一、引言二、Condition 接口概述2.1 基本概念2.2 与 Object 类等待通知方法的区别

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

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. 设备结构