0

在处理不同 API 级别的通知时,这些行出现错误。这就是我到目前为止所做的:

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

我不明白如何消除这些错误。请帮我。

编辑:我确实应用了如下编辑,但 NotificationCompact.Builer 也得到了不推荐使用getNotification()的返回 Notification 对象的方法。

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);
        builder.setContentIntent(contentIntent)
        .setSmallIcon(icon)
        .setTicker(text)
        .setWhen(System.currentTimeMillis())
        .setAutoCancel(true)
        .setContentTitle(title)
        .setContentText(text);

        notification = builder.getNotification();
        mNM.notify(NOTIFICATION, notification);
    }
4

2 回答 2

2

使用从 1.6 开始支持的支持库(V4 库)中的NotificationCompact.Bulide r

我认为这将解决您的问题。

于 2013-05-31T11:39:12.430 回答
0

最后,在这些人的帮助下,我最终找到了处理已弃用方法的解决方案:

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);
        }
于 2013-05-31T12:26:26.393 回答