setLatestEventInfo无法解析 [英] setLatestEventInfo cannot be resolved

查看:112
本文介绍了setLatestEventInfo无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在api级别10上发出通知,以下是我的代码,但无法获取语法错误setLatestEventInfo.我猜想这与API级别有关

I am trying to do a notification on api level 10 and below is my code but im getting a syntax error setLatestEventInfo cannot be resolved. I am guessing its something to do with the API level

 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)中删除了此方法.

If your compile SDK version is set to api 23+ you'll see this issue. This method was removed in M (api 23).

要从api 4开始进行通知,您可以使用 Android支持库 NotificationCompat.Builder >. Android文档有一个不错的示例:

To do notifications for api 4 onwards you can use the NotificationCompat.Builder which was added in the Android Support Library. The Android Documentation has a decent example:

 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版本.

You can replace "Notification.Builder" for "NotificationCompat" if need to support older api versions.

这篇关于setLatestEventInfo无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆