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

33

enter image description here文档显示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级别添加的,对吗? 添加于API 11级别


这是一个Lint警告,通知构建器需要API 16。请参考http://tools.android.com/tips/lint-checks。 - Raghunandan
1
向下滚动同一页并检查下面的主题公共方法。 - Raghunandan
谢谢。build方法在16级中被添加... - pt2121
5个回答

59

在这个答案中,感谢Cammille的评论。https://dev59.com/ZWw15IYBdhLWcg3wzO1_#8869647 - stinepike
文档上说 API 添加在 Level 11,这是指文档出现错误了吗? - pt2121
不是错误..请检查我在答案中提供的链接..上面明确写着该构建版本是来自API 16。 - stinepike
@EntryLevelDev 滚动到同一页底部(您在问题中发布的链接)并检查公共方法下面的主题。 - Raghunandan
好的,我明白了。是我的错误...我看到顶部的示例,所以我假设构建已添加在第11级。谢谢大家。 - pt2121

17

关于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")并不能完全隐藏它,但我认为这可能会有所帮助。


5

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

如果您的应用支持旧版本的Android,最低API级别为4,您可以使用位于Android支持库中的NotificationCompat.Builder替代。
关于支持库:

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


1
你可以使用以下内容:
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);
            }

0

我在Android Studio中遇到了同样的问题。我在build.gradle(module:app)文件中进行了小改动,问题得以解决。 enter image description here

同步项目后即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接