如何实现通知的弃用方法

我有一个小问题,但不明白如何摆脱这一点。

我创build了一个提供通知的类,但是这些行被标记为废弃:

... 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 } 

我可以写else部分?

Solutions Collecting From Web of "如何实现通知的弃用方法"

这是我如何结束了解决scheme:

 if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { notification = new Notification(icon, text, time); notification.setLatestEventInfo(this, title, text, contentIntent); // This method is removed from the Android 6.0 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); } 

编辑:

上述解决scheme工作。 不过,由于引入了NotificationCompat.Builder类,我们可以跳过if条件来检查比较当前的API版本。 所以,我们可以简单地删除if...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); 

这是获得关卡的正确方法。

  final int sdkVersion = Integer.parseInt(Build.VERSION.SDK); if (sdkVersion < Build.VERSION_CODES.HONEYCOMB) { ... 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 } 

所有的版本代码可以在这个开发者链接find。