我是靠谱客的博主 沉默鲜花,最近开发中收集的这篇文章主要介绍Android:通知:Not allowed to start service Intent / Bad notification for startForeground报错一报错二参考:一、android8.0 添加了前台所需要的权限二、启动服务方式适配 :三、startForeground启动通知,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
报错一
java.lang.RuntimeException:
Unable to start receiver 包名.MainReceiver:
java.lang.IllegalStateException:
Not allowed to start service Intent { cmp=包名/.MainService }:
app is in background uid UidRecord{90638 u0a10 RCVR idle change:
Android o需要适配服务
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(mIntent);
} else {
context.startService(mIntent);
}
报错二
android.app.RemoteServiceException:
Bad notification for startForeground:
java.lang.RuntimeException:
invalid channel for service notification:
Notification(channel=null pri=0
contentView=null vibrate=null sound=null
defaults=0x0 flags=0x40 color=0x00000000
vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)
Android o需要适配通知channel
参考:
在Android 8.0中使用Notification中发生 Bad notification for startForeground错误
一、android8.0 添加了前台所需要的权限
添加权限如下:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
二、启动服务方式适配 :
Intent mIntent = new Intent(context,MainService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(mIntent);
} else {
context.startService(mIntent);
}
三、startForeground启动通知
//service的onCreate调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
String CHANNEL_ID = "CHANNEL_ID";
String CHANNEL_NAME = "CHANNEL_ID";
NotificationChannel notificationChannel;
//进行8.0的判断
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(
CHANNEL_ID,
CHANNEL_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);
}
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.jianshu.com/p/14ba95c6c3e2"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(this, CHANNEL_ID)
.setTicker("Nature")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("这是一个测试标题")
.setContentIntent(pendingIntent)
.setContentText("这是一个测试内容")
.build();
}
notification.flags |= Notification.FLAG_NO_CLEAR;
//在service里再调用startForeground方法,不然就会出现ANR
startForeground(1, notification);
Log.e(TAG, "onStartCommand");
return Service.START_STICKY;
}
简化:
只通知栏显示
//service的onCreate调用
private void notification() {
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
notification = new Notification.Builder(this, CHANNEL_ID).build();
}
//在service里再调用startForeground方法,不然就会出现ANR
startForeground(1, notification);
}
最后
以上就是沉默鲜花为你收集整理的Android:通知:Not allowed to start service Intent / Bad notification for startForeground报错一报错二参考:一、android8.0 添加了前台所需要的权限二、启动服务方式适配 :三、startForeground启动通知的全部内容,希望文章能够帮你解决Android:通知:Not allowed to start service Intent / Bad notification for startForeground报错一报错二参考:一、android8.0 添加了前台所需要的权限二、启动服务方式适配 :三、startForeground启动通知所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复