public int onStartCommand(Intent intent,int flags,int startId){ long period = 60 *1000; if(timer==null){ timer=new Timer(); } timer.schedule(new TimerTask() { @Override public void run() { NotificationChannel channel = new NotificationChannel("channel","通知",NotificationManager.IMPORTANCE_DEFAULT); manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.createNotificationChannel(channel); builder = new NotificationCompat.Builder(TimeService.this); builder.setContentTitle("欢迎来到校园新闻app"); builder.setSmallIcon(R.drawable.collection); builder.setChannelId("channel"); Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); builder.setContentText("当前系统时间为:"+formatter.format(date)); builder.setAutoCancel(true); builder.setDefaults(Notification.DEFAULT_ALL); Notification notification = builder.build(); manager.notify(1,notification); } },0,period); return super.onStartCommand(intent, flags, startId); }在安卓API33版本无法加载通知,帮我修改一下这段代码
时间: 2024-02-14 16:17:30 浏览: 31
在 Android API 26(即 Android 8.0)及以上版本,通知渠道(Notification Channel)的设置是必须的,否则通知将不会被显示。因此,你需要在代码中添加通知渠道的设置。以下是修改后的代码:
```
public int onStartCommand(Intent intent, int flags, int startId) {
long period = 60 * 1000;
if (timer == null) {
timer = new Timer();
}
timer.schedule(new TimerTask() {
@Override
public void run() {
String channelId = "channel";
String channelName = "通知";
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
builder = new NotificationCompat.Builder(TimeService.this, channelId);
} else {
builder = new NotificationCompat.Builder(TimeService.this);
}
builder.setContentTitle("欢迎来到校园新闻app");
builder.setSmallIcon(R.drawable.collection);
builder.setContentText("当前系统时间为:" + DateFormat.getDateTimeInstance().format(new Date()));
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
manager.notify(1, notification);
}
}, 0, period);
return super.onStartCommand(intent, flags, startId);
}
```
这里添加了一个判断,如果当前 Android 版本大于等于 Android 8.0,就需要创建通知渠道并使用 channelId 构建 NotificationCompat.Builder 对象;否则,直接使用无参构造函数创建 NotificationCompat.Builder 对象即可。此外,我也修改了时间格式化的方式,使用了 DateFormat 类的 getDateTimeInstance() 方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)