转几篇文章方便以后看:
android8.0以上版本的前台服务
(四十二)Context.startForegroundService() did not then call Service.startForeground?
有关前台服务的startForegroundService接口和startForeground接口
解决办法
程序走到startForeground会报异常java.lang.RuntimeException的原因是需要添加个前台服务的权限android.permission.FOREGROUND_SERVICE
在manifest里加上这个即可:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
也可以创建和指定消息通道(在模拟器上测试,不创建好像也没事)