android.app.RemoteServiceException: Bad notification for startForeground
运行在模拟器上没有问题,运行在真机上就会报错,是安卓版本问题,使用Notificatio通知再Android8.0以上的的通知要设置渠道,否则就无法显示。所以在MyService中onCreatet如下代码:@Overridepublic void onCreate() {super.onCreate();String ID = "com.example.service1";//这里的id里面输入
·
运行在模拟器上没有问题,运行在真机上就会报错,是安卓版本问题,使用Notificatio通知再Android8.0以上的的通知要设置渠道,否则就无法显示。
所以在MyService中onCreatet如下代码:
@Override
public void onCreate() {
super.onCreate();
String ID = "com.sidebar.project"; //这里的id里面输入自己的项目的包的路径
String NAME = "LEFTBAR";
Intent intent = new Intent(MyService.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder notification; //创建服务对象
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(ID, NAME, manager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(channel);
notification = new NotificationCompat.Builder(MyService.this).setChannelId(ID);
} else {
notification = new NotificationCompat.Builder(MyService.this);
}
notification.setContentTitle("标题")
.setContentText("内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pendingIntent)
.build();
Notification notification1 = notification.build();
startForeground(1,notification1);
//manager.notify(1,notification1);
}
更多推荐
已为社区贡献2条内容
所有评论(0)