【发布时间】:2021-09-04 17:53:05
【问题描述】:
我有一个代码可以运行在一个版本上,但适用于最新版本。 Android工作室写道: “使用或覆盖已弃用的 API。使用 -Xlint:deprecation 重新编译以了解详细信息。”
我的代码是:
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Words reminder");
builder.setContentText("It's time to try yourself!");
builder.setSmallIcon(R.drawable.ic_stat_name);
builder.setAutoCancel(true);
builder.setPriority(Notification.PRIORITY_HIGH);
//builder.build().flags |= Notification.FLAG_AUTO_CANCEL;
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
Intent notifyIntent = new Intent(this, RightOrNot.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
builder.setContentIntent(pendingIntent);
Notification notificationCompat = builder.build();
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
managerCompat.notify(NOTIFICATION_ID, notificationCompat);
我相信问题出在第一行:
Notification.Builder builder = new Notification.Builder(this);
您知道如何修复线路或使用适用于所有版本的不同方法吗?
【问题讨论】:
-
这只是一个警告。你范忽略它。
-
但是在其他使用最新版本的手机上不行
-
你必须为oreo及以上版本创建一个通知渠道。
标签: java android notifications