【问题标题】:Android: Error on notification API levelAndroid:通知 API 级别的错误
【发布时间】:2014-03-19 08:53:06
【问题描述】:

我正在开发一个应用程序,我在其中使用推送通知,这工作正常。我的 API 级别最低为 8,最高为 19。

我的生成通知代码如下:

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, SplashScreenActivity.class);
    notificationIntent.putExtra("message", message);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}

我在两个地方收到警告:

Notification notification = new Notification(icon, message, when);

notification.setLatestEventInfo(context, title, message, intent);

警告是: 构造函数 Notification(int, CharSequence, long) 已弃用分别不推荐使用 Notification 类型的方法 setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)

所以我将代码更改为:

Notification notification;

    int currentVersion = android.os.Build.VERSION.SDK_INT;
    int honeycombVersion = android.os.Build.VERSION_CODES.HONEYCOMB;

    if (currentVersion >= honeycombVersion ){
        notification = new Notification.Builder(context)
                                        .setContentTitle(",App")
                                        .setContentText(message)
                                        .setSmallIcon(icon)
                                        .build();
    } else{
        notification = new Notification(icon, message, when);
    }

但现在我在 if 中遇到错误:

调用需要 API 级别 11(当前最低为 8):新的 android.app.Notification.BuilderElse 部分中的警告保持不变。

现在该怎么办,请指导。

【问题讨论】:

    标签: android notifications deprecated android-notifications


    【解决方案1】:

    在您的情况下,您可以做的最好的就是添加此检查

    int apiVersion = android.os.Build.VERSION.SDK_INT;
    if (apiVersion >= Build.VERSION_CODES.HONEYCOMB){
       // call the constructor available since api level 11
    } else {
      // fallback call the old constructor
    }
    

    并添加SuppressLint 注释以使警告/错误消失

    【讨论】:

      【解决方案2】:

      对于“Call requires API level 11 (current min is 8): new android.app.Notification.Builder”的错误

      尝试在您的 build.gradle 上将此 minSdkVersion 8 更改为 minSdkVersion 11

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多