Android 使用Notification.Builder时获取错误
在处理不同API级别的通知时,这些行出现错误。到目前为止,我是这样做的:Android 使用Notification.Builder时获取错误,android,android-notifications,Android,Android Notifications,在处理不同API级别的通知时,这些行出现错误。到目前为止,我是这样做的: ... int currentapiVersion = android.os.Build.VERSION.SDK_INT; Notification notification; if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){ notification = new Notification
...
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);
}
...
使用支持1.6的支持库(V4库)中的r
我认为这将解决您的问题。最后,在这些人的帮助下,我最终找到了处理不推荐的方法的解决方案:
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);
}
if(currentapiVersion
您可以从@anilmetagg尝试是。在这个SO问题中,建议使用NotificationCompact.Builer
,但它也有弃用的方法getNotification()
。我使用了这个类,但这个类也有弃用的方法getNotification()
通过它我可以获得通知
对象。在developer.android.com
上,他们建议使用build()
方法,但我不知道如何使用,因为build方法不带参数。我不知道如何使用now.getNotification。不推荐使用build方法,但api级别16支持该方法。最好忽略这个类并使用NotificationCompact.Builder,如果您想从api级别4获得支持,我非常感谢您的建议。投票通过。如果你想演示,请检查这个链接,我认为这将帮助你
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);
}