【Android异常】关于Notification启动时,startForeground报错
遇到两个报错:
第一个权限问题报错,好解决
startForeground requires android.permission.FOREGROUND_SERVICE
Manifest给下权限就行
<manifest ...>
...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
...
<application ...>
...
</manifest>
第二个问题,Android 8.0以上需要Notification需要设置个Channel
android.app.RemoteServiceException: Bad notification for startForeground
解决方法如下:原博客
//以下为新增---------------------------------------------
String CHANNEL_ONE_ID = "com.primedu.cn";
String CHANNEL_ONE_NAME = "Channel One";
NotificationChannel notificationChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_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);
}
//--------------------------------------------------------以上为新增 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification = new Notification.Builder(this).setChannelId(CHANNEL_ONE_ID)
.setTicker("Nature")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("xxxx")
.setContentText(musicList.size() > 0 && musicList != null ? musicList.get(currentMusic).radio_en_desc:"xxxxx")
.setContentIntent(pendingIntent)
.getNotification();
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(1, notification);
我标记了一下哪些地方是新增的
同新增了一句.setChannelId(CHANNEL_ONE_ID)
就ok了,在运行就没问题了,通知正常开启
【Android异常】关于Notification启动时,startForeground报错的更多相关文章
- springboot工程启动时,报错:No bean named 'shiroFilter' available
在启动Springboot项目时,报错:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ' ...
- Oracle-11g 数据库启动时,报错"ORA-01092"及"ORA-18008: cannot find OUTLN schema"
适用情形: Oracle-11g 数据库启动时,出现类似如下错误. ORA-01092: ORACLE instance terminated. Disconnection forced ORA-18 ...
- web项目——启动时tomcat报错:Server Tomcat v7.0 Server at localhost failed to start.
报错信息:Server Tomcat v7.0 Server at localhost failed to start. 报错截图: 原因分析:在使用SSM框架时,生成的mapping与系统配置文件不 ...
- egg启动时,报错:Ignoring invalid timezone passed to Connection的解决方案
报错信息 Ignoring invalid timezone passed to Connection: +8:00. This is currently a warning, but in futu ...
- Tomcat 启动时项目报错 org.springframework.beans.factory.BeanCreationException
事情是这样的,最近我们公司需要将开发环境和测试环境分开,所以就需要把所有的项目部署一套新的开发环境. 我们都是通过 Jenkins 进行部署的,先说一下两个环境的配置: 测试环境配置(旧):jdk1. ...
- 【问题解决:SFL4J】启动时SLF4J报错
问题描述 启动时报错 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Default ...
- eclipse debug启动时tomcat报错
Class.getDeclaredConstructors0(boolean) line: not available [native method] tomcat debug启动突然启动不起来 停 ...
- No Spring WebApplicationInitializer types detected on classpath。启动时不报错,但是页面打不开。
一片红,没有黑色disPatcher的加载. 百度,但是没有用,二十分钟浪费,这个问题的本质就是web.xml中的disPatcher没有加载,但是我肯定和代码无关,配置文件也没有变化过,值可能是to ...
- ideal的maven工程启动时老是报错,提示web.xml里面的监听器找不到,但是实际又是存在的
-X clean compile package -Dmaven.repo.local=D:\repository-pss -Dmaven.test.skip=true maven仓库地址
- 三、SpringBoot启动时JDBC报错:You must configure either the server or JDBC driver (via the serverTimezone configuration property)
错误提示: Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connectio ...
随机推荐
- TCP&UDP测试工具使用方式方法
第一项:测试TCP 步骤1:首先需要一台电脑上建立一个TCP服务器.并设置一个端口号如下图60003即可.并开启服务 步骤2:在另外一台电脑上,创建一个客户端连接,如下图.输入服务器的IP地址和端口号 ...
- selenium grid4.0+分布式的部署配置(hub/node方式)
一.grid安装 本文已jar包的方式讲解,访问并下载jar包:Releases · SeleniumHQ/selenium · GitHub 二.grid配置 假设主机:10.1.5.219,从机: ...
- Git客户端部署使用-生成ssh密钥2
1. 设置用户名 其中双引号中的 XXX 是用户名,记得替换成自己的用户名,需要注意的是这里的用户名是git服务器所使用的名称,一般公司用的都是员工名称的全拼. # 在git窗口中输入命令行: git ...
- 解决sqlplus中方向键和退格键乱码问题
centos7 1.安装rlwrap软件包 yum install rlwrap -y 2.编辑环境变量 su - oracle vim ~/.bash_profile alias sqlplus=' ...
- UVM reg model 常见问题记录
1.仿真log中报出大量的"include_coverage not located, did you mean ***"? (1) user在构建register model或者 ...
- BUUCTF-[GXYCTF2019]Ping Ping Ping
一道命令执行题目 一.基础知识 Linux shell特殊字符(参考链接) [;]作为多个命令语句的分隔符(Command separator [semicolon]). 要在一个语句里面执行 ...
- sql使用!=查询时会忽略null数据
table_a有3条数据 column1值分别为1,0,null 那么 select * from table_a where column1!='1' 只会查到clumn1为0的数据,null的数据 ...
- VMware Fusion Pro 13.0.0 最新序列号【转】
Fusion简介 VMware Fusion是最好的Windows-to-Mac解决方案,是任何平台上最强大的应用开发和测试工具. Fusion 13支持macOS 13.0,为开发者.IT管理员和普 ...
- c++基础: uint8_t uint16_t uint32_t uint64_t size_t ssize_t数据类型
https://blog.csdn.net/lzx_bupt/article/details/7066577 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个 ...
- SAP日志表 CDHDR和CDPOS
1. 标准日志表CDHDR 和 CDPOS OBJECTCLAS = 'INFOSATZ' 信息记录 OBJECTCLAS = 'BANF' 采购申请 OBJECTCLAS = 'EINKBELEG' ...