gpt4 book ai didi

android - setLatestEventInfo 无法解析

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:20:02 25 4
gpt4 key购买 nike

我正在尝试在 api 级别 10 和以下是我的代码,但我收到语法错误 setLatestEventInfo 无法解决。我猜它与 API 级别有关

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

最佳答案

如果您的编译 SDK 版本设置为 api 23+,您将看到此问题。此方法已在 M (api 23) 中删除。

要从 api 4 开始通知,您可以使用 NotificationCompat.Builder这是在 Android Support Library 中添加的. Android Documentation有一个像样的例子:

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

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

关于android - setLatestEventInfo 无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32450449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com