android开发分享Android8.0之后启动服务崩溃

1、启动服务时候if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForegroundService(new Intent(this, BleNfcDeviceService.class));} else {startService(new Intent(this, BleNfcDeviceService.class));}2、服务接受时notificationManager = (NotificationManager)

1、启动服务时候
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(this, BleNfcDeviceService.class));
} else {
startService(new Intent(this, BleNfcDeviceService.class));
}
2、服务接受时
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
startForeground(1, getNotification());

private Notification getNotification() {
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setContentTitle(“”)
.setContentText(“”);

    //设置Notification的ChannelID,否则不能正常显示      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {         builder.setChannelId(notificationId);     }     Notification notification = builder.build();     return notification;  } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/896567.html

(0)
上一篇 2021年10月22日
下一篇 2021年10月22日

精彩推荐