android中的Notification报错title
我今天调试发现运行在测试手机上就报错这个错,我是一加8的 13的系统,我自己那虚拟手机13,12测试了一下并没有报这个错只有我在自己手机上报这个错,有没有大佬知道哪错了,app_name是有的
service中服务
public class MyService extends Service {
private int notificationId;
@Override
public void onCreate() {
notification();
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void notification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationId = new Random().nextInt();
//自己生成的用于通知栏的channelId,高版本必备
String channelId = String.valueOf(new Random().nextInt());
NotificationChannel mChannel = new NotificationChannel(channelId, "name", NotificationManager.IMPORTANCE_HIGH);
mChannel.enableVibration(false);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
manager.createNotificationChannel(mChannel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
builder.setWhen(System.currentTimeMillis()) //设置通知时间戳
.setContentTitle("重新开启运行")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText("开启时间:" + DateFormat.format("yyyy-MM-dd HH:mm:ss", Calendar.getInstance(Locale.CHINA)))
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
Notification notification;
notification = builder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
startForeground(notificationId, notification);
} else {
manager.notify(notificationId, notification);
}
Toast.makeText(this, "守护进程:开启", Toast.LENGTH_SHORT).show();
}
}
MainActivity中的启动
Intent intent = new Intent(this, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
本作品采用《CC 协议》,转载必须注明作者和本文链接