遇到两个报错:

第一个权限问题报错,好解决

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报错的更多相关文章

  1. springboot工程启动时,报错:No bean named 'shiroFilter' available

    在启动Springboot项目时,报错:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ' ...

  2. Oracle-11g 数据库启动时,报错"ORA-01092"及"ORA-18008: cannot find OUTLN schema"

    适用情形: Oracle-11g 数据库启动时,出现类似如下错误. ORA-01092: ORACLE instance terminated. Disconnection forced ORA-18 ...

  3. web项目——启动时tomcat报错:Server Tomcat v7.0 Server at localhost failed to start.

    报错信息:Server Tomcat v7.0 Server at localhost failed to start. 报错截图: 原因分析:在使用SSM框架时,生成的mapping与系统配置文件不 ...

  4. egg启动时,报错:Ignoring invalid timezone passed to Connection的解决方案

    报错信息 Ignoring invalid timezone passed to Connection: +8:00. This is currently a warning, but in futu ...

  5. Tomcat 启动时项目报错 org.springframework.beans.factory.BeanCreationException

    事情是这样的,最近我们公司需要将开发环境和测试环境分开,所以就需要把所有的项目部署一套新的开发环境. 我们都是通过 Jenkins 进行部署的,先说一下两个环境的配置: 测试环境配置(旧):jdk1. ...

  6. 【问题解决:SFL4J】启动时SLF4J报错

    问题描述 启动时报错 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Default ...

  7. eclipse debug启动时tomcat报错

    Class.getDeclaredConstructors0(boolean) line: not available [native method] tomcat  debug启动突然启动不起来 停 ...

  8. No Spring WebApplicationInitializer types detected on classpath。启动时不报错,但是页面打不开。

    一片红,没有黑色disPatcher的加载. 百度,但是没有用,二十分钟浪费,这个问题的本质就是web.xml中的disPatcher没有加载,但是我肯定和代码无关,配置文件也没有变化过,值可能是to ...

  9. ideal的maven工程启动时老是报错,提示web.xml里面的监听器找不到,但是实际又是存在的

    -X clean compile package -Dmaven.repo.local=D:\repository-pss -Dmaven.test.skip=true maven仓库地址

  10. 三、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 ...

随机推荐

  1. springcloud(五) - 网关gateway

    功能介绍 springcloud gateway提供一种以路由的方式,基于Filter链的方式提供网关的基本功能.如安全.监控.限流. 网关:将不同协议的网络段连接到一起的设备,外网进入内网的入口,对 ...

  2. grep的正则匹配使用方式

    grep正则匹配的命令方式如下 $ grep 'pattern1\|pattern2' filename 或 $ grep -E 'pattern1|pattern2' file grep默认是区分大 ...

  3. ADC采样信号RMS测量值的Verilog实现

    术语"RMS"代表"Root-Mean-Squared".大多数书籍将此定义为"产生与等效直流电源相同的加热效果的交流电量",或者沿着这些线 ...

  4. 双CAN通讯模板

    本文中CAN1采用16位掩码,CAN2只能使用大于等于14的筛选器组,根据手册只能使用32位的掩码(因为大于14的筛选器没有说明). 经测试,STM32手册中的筛选器配置图错误,库函数的命名正确.参考 ...

  5. python bottle小记

    # coding=utf-8import bottle @bottle.route('/url/url', method=['GET','POST'])def big_data(): # 获取请求参数 ...

  6. 蓝桥杯训练赛二-1199 问题 E: 哥德巴赫曾猜测

    题目描述 德国数学家哥德巴赫曾猜测:任何大于6的偶数都可以分解成两个素数(素数对)的和.但有些偶数可以分解成多种素数对的和,如: 10=3+7,10=5+5,即10可以分解成两种不同的素数对 输入 输 ...

  7. 【Anaconda】Jupyter 中添加 Anaconda 环境

    两种方法: 1. 安装 nb_conda_kernels,将所有 conda 环境同步至 Jupyter Notebook,参考『Jupyter notebook选择conda环境 - 简书』. 2. ...

  8. vue-图书管理系统

    /* 路由模块 */ const express = require('express'); const router = express.Router(); const service = requ ...

  9. Mosquitto安装与部署

    版本说明: Mosquitto版本:v2.0.10     libwebsockets版本:v3.0.1(用于支持websockets)     mosquitto-go-auth(Mosquitto ...

  10. vue element-ui form验证中自定义验证方式通过不返回true问题

    项目中使用了element-ui的form验证,自定义了手机号的验证规则,验证不通过的时候定义了callback()扔出错误.但是忘了写通过的callback().导致form验证通过拿不到返回的va ...