【问题标题】:Call requires API level 16 (current min is 14): android.app.Notification.Builder#build调用需要 API 级别 16(当前最低为 14):android.app.Notification.Builder#build
【发布时间】:2013-05-08 16:37:23
【问题描述】:

文档说 Notification.Builder 是在 API 级别 11 中添加的。 为什么我会收到此 lint 错误?

调用需要 API 级别 16(当前最低为 14):android.app.Notification.Builder#build

notification = new Notification.Builder(ctx)
                .setContentTitle("Title").setContentText("Text")
                .setSmallIcon(R.drawable.ic_launcher).build();

清单:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

我错过了什么吗?

如果我错了,请纠正我,但 API 是在第 11 级添加的,对吗​​? Added in API level 11

【问题讨论】:

  • tools.android.com/tips/lint-checks。它的 lint 警告通知生成器需要 api 16
  • 向下滚动同一页面并查看公共方法下方的主题
  • 谢谢。 build 方法是在第 16 级添加的...

标签: android android-lint


【解决方案1】:

NotificationBuilder.build() 需要 API 级别 16 或更高。 API 级别 11 和 15 之间的任何内容都应使用 NotificationBuilder.getNotification()。所以使用

notification = new Notification.Builder(ctx)
                .setContentTitle("Title").setContentText("Text")
                .setSmallIcon(R.drawable.ic_launcher).getNotification();

【讨论】:

  • 礼貌评论 Cammille 在这个答案stackoverflow.com/a/8869647/931982
  • 你的意思是文件有误?它说 api 是在 11 级添加的。
  • 没有错..检查我在回答中提供的链接..有明确的书面说明构建来自 api 16。
  • @EntryLevelDev 向下滚动同一页面(您在问题中发布的链接)并检查公共方法下方的主题。
  • 好的,知道了。我的错误...我看到了顶部的示例,所以我假设构建是在 11 级中添加的。谢谢大家。
【解决方案2】:

关于 API 级别需要为 16 的提示是正确的。这对我有用

if (Build.VERSION.SDK_INT < 16) {
    nm.notify(MY_NOTIFICATION_ID, notificationBuilder.getNotification());
} else {
    nm.notify(MY_NOTIFICATION_ID, notificationBuilder.build());
}

我遇到了通知在较新的设备上运行良好但在 Android 4.0.4(API 级别 15)上运行良好的问题。我确实收到了 Eclipse 的弃用警告。 @SuppressWarnings("deprecation") 并没有完全隐藏它,但我认为这可能会有所帮助。

【讨论】:

    【解决方案3】:

    Android Lint 是 ADT 16(和 Tools 16)中引入的一个新工具,它可以扫描 Android 项目源代码中的潜在错误。它既可以作为命令行工具使用,也可以与 Eclipse 集成

    http://tools.android.com/tips/lint

    对于 lint 检查列表

    http://tools.android.com/tips/lint-checks

    用于抑制 lint 警告

    http://tools.android.com/tips/lint/suppressing-lint-warnings

    http://developer.android.com/reference/android/app/Notification.Builder.html

    如果您的应用支持早于 API 级别 4 的 Android 版本,您可以改用 Android 支持库中提供的 NotificationCompat.Builder。

    支持库

    http://developer.android.com/tools/extras/support-library.html

    【讨论】:

      【解决方案4】:

      您可以从此使用:

      if (Build.VERSION.SDK_INT < 16) {
                      Notification n  = new Notification.Builder(this)
                              .setContentTitle("New mail from " + "test@gmail.com")
                              .setContentText("Subject")
                              .setSmallIcon(R.mipmap.ic_launcher)
                              .setContentIntent(pIntent)
                              .setAutoCancel(true).getNotification();
                      NotificationManager notificationManager =
                              (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                      notificationManager.notify(0, n);
                  } else {
                      Notification n  = new Notification.Builder(this)
                              .setContentTitle("New mail from " + "test@gmail.com")
                              .setContentText("Subject")
                              .setSmallIcon(R.mipmap.ic_launcher)
                              .setContentIntent(pIntent)
                              .setAutoCancel(true).build();
                      NotificationManager notificationManager =
                              (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                      notificationManager.notify(0, n);
                  }
      

      【讨论】:

        【解决方案5】:

        我在 android studio 中遇到了同样的问题。我在 build.gradle(module:app) 文件中做了一些小改动,问题已经解决。

        同步项目后。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-16
          • 1970-01-01
          • 1970-01-01
          • 2017-02-23
          相关资源
          最近更新 更多