【问题标题】:How to implement the deprecated methods of Notification如何实现Notification的弃用方法
【发布时间】:2013-05-31 07:44:56
【问题描述】:

我有一个小问题,但不明白如何解决这个问题。

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

...
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 部分我可以写什么??

【问题讨论】:

  • 是的,你可以。确保将 targetSdkVersion 设置为 11 或更多。
  • @Tarun 是的,我的目标 sdk 版本设置为“17”,如果“if...else”条件可以处理这个问题,请您提供示例代码或参考,以便我可以得到一点提示。
  • 您的代码对我来说似乎是正确的。如果 else 将按照 api 级别处理。
  • @Tarun 这只是一个想法,我想知道我是否可以写这个。但是如何获取当前的 API 版本,该设备可能正在使用...
  • Integer.valueOf(android.os.Build.VERSION.SDK);

标签: android android-notifications


【解决方案1】:

这就是我最终得到解决方案的方式:

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);
        }

编辑:

上述解决方案有效。尽管如此,由于引入了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);

【讨论】:

  • 我用过NotificationCompact.Builder
  • 仅供参考 setLatestEventInfo() 在 Android 6.0 中已从 SDK 中删除。 source
  • 时间应该是 System.currentTimeMillis() 如果你想要当前时间。 stackoverflow.com/a/27721837/211457
  • 在哪里可以找到 NotificationCompat.Builder?我如何将它包含在我的项目中?编辑:它在 android.support.v4
【解决方案2】:

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

 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
} 

所有版本代码都可以在developer link找到。

【讨论】:

  • 嘿,我在你在其他部分写的这些行中有错误,告诉call requires API level 11 or more
  • Build.VERSION_CODES.HONEYCOMB = 11 所以只有当 api 大于 11 时才会调用 else 部分。
  • 你很好。但是如何消除这些错误?有了这个,我什至无法运行我的项目。请给我一些解决方案...
  • 错误是什么?你能把它贴在这里吗...也添加你的清单 uses-sdk。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
相关资源
最近更新 更多