遇到两个报错:

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

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. Springboot开发微信支付API-V3

    前段时间因为项目需要对接微信支付,原本打算拿之前开发好的代码用就行了,后面发现微信支付升级API-V3了,和V2相比安全措施多了很多.最麻烦的就是各种证书的管理.加载. 作者自己也对接过N多支付系统了 ...

  2. golang 日志

    package log import ( "NOONASN/global" "github.com/natefinch/lumberjack" "go ...

  3. MySQL 列定义的类型是varchar,已建立索引,查询时如果传入的是数字,则无法利用索引,查询特别慢。

    类型不对,导致无法充分利用索引. 比如:select * from table_name_xxx where name = "1234";  ----  查询很快 ,能够使用到na ...

  4. MQTT 发布/订阅模式介绍

    MQTT 发布/订阅模式 发布订阅模式(Publish-Subscribe Pattern)是一种消息传递模式,它将发送消息的客户端(发布者)与接收消息的客户端(订阅者)解耦,使得两者不需要建立直接的 ...

  5. window向linux传递文件

    1.需要在window建一个ftp的server 2.在linux的指定目录下输入: curl ftp://192.168.98.90/libalgorithm.so -u "embftp: ...

  6. GitBook的使用备忘

    GitBook环境搭建 npm install -g gitbook-cli # 新建目录,如helloworld cd helloworld # 执行此语句,需等待一段时间 gitbook init ...

  7. go-浅学设计模式随记

    责任链模式 组成:由多个处理器及处理器处理标志串联组成 作用:常用于处理流水线事务,利用多个处理器对同一个对象进行处理,可以利用各处理器开关 场景:常见逻辑层处理逻辑:获取参数.fetch数据.逻辑处 ...

  8. Linux下mysql安装教程

    一 环境准备 1.检查是否已经安装过mysql,执行命令 [root@localhost /]# rpm -qa | grep mysql 从执行结果,可以看出我们已经安装了mysql-libs-5. ...

  9. Bug_Android error: duplicate attribute. (错误:重复属性。)

    error: duplicate attribute. 错误:重复属性. 如报错信息提示,属性重复赋值了 场景:刚拿到项目源码,进行修改前,先设置它的背景色,区分是哪一块内容,但没看到组件已定义了背景 ...

  10. 30.zookeeper部署

    (一)Zookeeper基础知识.体系结构.数据模型 1 zookeeper是一个类似linux.hdfs的树形文件结构,zookeeper可以用来保证数据在(zk)集群之间的数据的事务性一致. 2 ...