如何修复方法setLatestEventInfo(Context,String,String,PendingIntent)未定义类型通知

时间:2015-11-22 17:11:58

标签: java android eclipse

我无法解决我在Eclipse中遇到的这个错误。这是一个进口项目。除了这个错误之外,其他所有工作正常:

  

方法setLatestEventInfo(Context,String,String,PendingIntent)未定义类型Notification MyNotificationBuilderToGingerBread.java / CANAPP / src / com / ftech / canapp / notification line 14 Java问题

这是我正在处理的代码

package com.ftech.canapp.notification;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;

public class MyNotificationBuilderToGingerBread {

    Notification notification = null;

    @SuppressWarnings("deprecation")
    public MyNotificationBuilderToGingerBread(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent) {
        notification = new Notification(icon, ticker, timeStamp);
        notification.setLatestEventInfo(myContext, title, info, pendingIntent);
    }

    @SuppressWarnings("deprecation")
    public MyNotificationBuilderToGingerBread(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent, int flags) {
        notification = new Notification(icon, ticker, timeStamp);
        notification.setLatestEventInfo(myContext, title, info, pendingIntent);
    }

    public Notification get() {
        return notification;
    }

}

2 个答案:

答案 0 :(得分:1)

我已经为所有版本使用了这个包装器:

public static Notification notification(Context ctx, String title, int icon,
            long when, PendingIntent pIntent, String contentTitle,
            String contentText,int flags, int defaults){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
        Notification notification = builder.setContentIntent(pIntent)
              .setSmallIcon(icon).setTicker(title).setWhen(when).setContentTitle(contentTitle)
              .setContentText(contentText).setDefaults(defaults).build();
        notification.flags = flags;
        return notification;
    }

答案 1 :(得分:0)

方法setLatestEventInfo(Context,String,String,PendingIntent)在先前版本的Android中已弃用,并已在API v23中删除。如果您定位该版本的Android,则无法使用该方法。

编辑:为了更新通知信息,您可以使用NotificationManager.notify(),这是API 23的一部分。