遇到两个报错:

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

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. windows用curl报错

    https://www.shuzhiduo.com/A/kmzLRmgl5G/ IE浏览器 -> 设置 -> Internet选项 -> 安全 -> 本地Internet -& ...

  2. gird 布局控制元素都显示在一行

    gird 布局控制元素都显示在一行 <ul class="list"> <li v-for="(li, index) in list" :ke ...

  3. git submodule .gitmodules 子模块

    初始化 git submodule init 添加子项目 git submodule add -b xxxx {path} -b 指定分支 path 可为空 (不知为何, 直接写在 .gitmodul ...

  4. PCB Layout之EMMC_Flash走线总结@@@

    PCB Layout之EMMC_Flash走线总结 1,数据线DATA[0-7]走线要(基本)等长(含芯片内部线长),线要短,线间距控制3W原则,参考面要完整(参考面下面最好不要走其它高速信号线),阻 ...

  5. c#笔记(四)——switch

    ---恢复内容开始--- using UnityEngine; using System.Collections;   public class Script1 : MonoBehaviour {   ...

  6. git clone 指定分支/指定commit

    方法一 下载整个branch及历史记录,文件较大,耗时 git clone --depth 1 [git-url] -b [branch-name] git reset --hard [commit- ...

  7. iptables(二)常用规则即操作示例

    常用规则示例 修改chain默认策略 #filter表在INPUT chain默认策略为ACCEPT[root@iptables_host02 ~]# iptables -nvL INPUTChain ...

  8. sublime 设置快捷键

    Tools-> Developer-> New Snippet 打开后保存文件要是以 .sublime-snippet 做结尾 <snippet> <content> ...

  9. sublime自动添加注释

    Step1: 需要使用组合键command+shift+p 搜索Install Package 后回车 确定 Step2: 经过上一步回车后到达第二步(可能需要等待一会儿),会出现搜索框,搜索DocB ...

  10. 批量导入数据到redis--速度极快

    ##批量导入数据到redis1)创建含有redis命令的文本并转换格式vim test.txt-------------------------HMSET provinceblack:14716261 ...