今天更新了android8.0 并且在一個8.0的系統上運作,居然報錯了
報錯日志就是标題這個了
也就是使用startForeground方法開啟通知的時候報錯的
在android8.0後 需要給notification設定一個channelid不要問我為什麼 解決方法如下
//新增---------------------------------------------
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了,在運作就沒問題了,通知正常開啟