如何实现Notification的弃用方法

Chi*_*oni 31 android android-notifications

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

我创建了一个用于提供通知的类,但这些行被标记为已弃用:

...
Notification notification = new Notification(icon, text, time); // deprecated in API level 11
...
notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
...
Run Code Online (Sandbox Code Playgroud)

替代方法是:

...
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
...
Run Code Online (Sandbox Code Playgroud)

我可以编写类似的代码:

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
    ...
}
Run Code Online (Sandbox Code Playgroud)

我提供的最小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
        }
Run Code Online (Sandbox Code Playgroud)

我能为else部分写些什么?

Chi*_*oni 76

这就是我最终解决方案的方法:

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);
        }
Run Code Online (Sandbox Code Playgroud)

编辑:

以上解决方案有效.但是,由于NotificationCompat.Builder引入了类,我们可以跳过检查比较当前API版本的if条件.所以,我们可以简单地删除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);
Run Code Online (Sandbox Code Playgroud)

  • FYI`setLatestEventInfo()`已从Android 6.0中的SDK中删除.[源(http://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.html#android.app.Notification.setLatestEventInfo_removed%28android.content.Context,%20java.lang.CharSequence ,%20java.lang.CharSequence,%20android.app.PendingIntent%29) (15认同)

Tar*_*run 6

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

 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
} 
Run Code Online (Sandbox Code Playgroud)

可以在此开发人员链接中找到所有版本代码.