本文主要是介绍Android:通知:Not allowed to start service Intent / Bad notification for 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方法,不然就会出现ANRstartForeground(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方法,不然就会出现ANRstartForeground(1, notification);
}
报错 no icon :
ActivityManager:
Attempted to start a foreground service (com.xxx.xxxx/.widget.WidgetService) with a broken notification (no icon:
经过查找源码:
- SDK_INT< 18, postNotification()没有判断iflocalForegroundNoti.icon;
- 18 =< SDK_INT < 23, postNotification()中if (localForegroundNoti.icon == 0);
- 23 =< SDK_INT, postNotification()中if (localForegroundNoti.getSmallIcon() == null);
小于APi 18,传一个new Notification()是不会显示通知的。
大于等于18就不行了,但是可能通过在启动一个service,然后startForeground一个相同的id, 然后立刻stopForeground(true),可以将其消失(注意:这其实是google的bug)。
实测android 6.0生效。
但是在7.1.2上,这种方法已经失效了,所以,这些版本无法解决。
1.更好的方法是startForeground一个真实有效的notification过去。
2.startForeground只要传的id相同,不管是不是一个进程,不管是不是同一个notification,。
都会用最新的notification覆盖旧的,只显示一个。
这篇关于Android:通知:Not allowed to start service Intent / Bad notification for startForeground的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!