android 弃用方法,android – 如何实现已弃用的通知方法
我有一个小问题,但不明白如何摆脱这一点。我创建了一个类来提供通知,但这些行标记为已弃用:...Notification notification = new Notification(icon, text, time); // deprecated in API level 11...notification.setLatestEventInfo(this, title, text, conten
我有一个小问题,但不明白如何摆脱这一点。
我创建了一个类来提供通知,但这些行标记为已弃用:
...
Notification notification = new Notification(icon, text, time); // deprecated in API level 11
...
notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
...
替代方法是:
...
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build(); // available from API level 11 and onwards
...
我可以写一个像下面的代码:
if(API_level < 11)
{
...
Notification notification = new Notification(icon, text, time); // deprecated in API level 11
...
notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
...
}
else
{
...
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build(); // available from API level 11 and onwards
...
}
我提供的最小sdk版本为“8”。
编辑:
我喜欢下面:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
Notification 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
{
// what to write here
}
我可以写什么部分?
更多推荐
所有评论(0)