2015-09-08 133 views
5

我想在api级别10上做一个通知,下面是我的代码,但即时通讯语法错误setLatestEventInfo无法解析。我猜测它与API级别有关setLatestEventInfo无法解析

public static void sendNotification(Context caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents, boolean sound, boolean flashLed, boolean vibrate, int iconID) { 
    NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE); 

    final Notification notify = new Notification(iconID, "", System.currentTimeMillis()); 

    notify.icon = iconID; 
    notify.tickerText = title; 
    notify.when = System.currentTimeMillis(); 
    notify.number = numberOfEvents; 
    notify.flags |= Notification.FLAG_AUTO_CANCEL; 
    if (sound) notify.defaults |= Notification.DEFAULT_SOUND; 

    if (flashLed) { 
     // add lights 
     notify.flags |= Notification.FLAG_SHOW_LIGHTS; 
     notify.ledARGB = Color.BLUE; 
     notify.ledOnMS = 500; 
     notify.ledOffMS = 500; 
    } 

    if (vibrate) { 
     notify.vibrate = new long[]{100, 200, 300}; 
    } 

    Intent toLaunch = new Intent(caller, activityToLaunch); 
    toLaunch.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
    toLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent intentBack = PendingIntent.getActivity(caller, notify.number, toLaunch, 0); 
    notify.setLatestEventInfo(caller, title, msg, intentBack); 
    notifier.notify(notify.number, notify); 
    notify.number = notify.number + 1; 
} 

回答

8

如果你的编译SDK版本设置为api 23+,你会看到这个问题。 M(api 23)中删除了此方法。

要做到的API 4通知起,可以使用这是在Android Support Library添加NotificationCompat.Builder。该Android Documentation有一个体面的例子:

Notification noti = new Notification.Builder(mContext) 
     .setContentTitle("New mail from " + sender.toString()) 
     .setContentText(subject) 
     .setSmallIcon(R.drawable.new_mail) 
     .setLargeIcon(aBitmap) 
     .build(); 

您可以替换“Notification.Builder”为“NotificationCompat”如果需要支持旧的API版本。

+0

如果您不使用支持库,因为它会将兆字节添加到您的应用程序大小,该怎么办? – Justin