我在处理不同API级别的通知时遇到这些行的错误。这就是我到目前为止所做的:
...
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
notification = new Notification(icon, text, time);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
else
{
notification = new Notification.Builder(this) // error
.setContentTitle(title) // in
.setContentText(tmp_task_brief) // these
.setSmallIcon(icon) // lines
.setLargeIcon(null) // telling "this method call requires API level 11
.build(); // or higher"
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
...
我不明白如何删除这些错误。请帮帮我。
编辑:我确实按照以下方式应用了编辑,但NotificationCompact.Builer也不推荐使用返回Notification对象的方法getNotification()
。
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB)
{
notification = new Notification(icon, text, time);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
else
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(contentIntent)
.setSmallIcon(icon)
.setTicker(text)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(text);
notification = builder.getNotification();
mNM.notify(NOTIFICATION, notification);
}
答案 0 :(得分:2)
答案 1 :(得分:0)
最后,在这些人的帮助下,我最终找到了处理弃用方法的解决方案:
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification(icon, text, time);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(text).setWhen(time)
.setAutoCancel(true).setContentTitle(title)
.setContentText(text).build();
mNM.notify(NOTIFICATION, notification);
}