Call requires API level 16 (current min is 14): android.app.Notification.Builder#build

StackOverflow https://stackoverflow.com/questions/16445805

  •  14-04-2022
  •  | 
  •  

Frage

enter image description hereThe documentation says Notification.Builder is Added in API level 11. Why I get this lint error?

Call requires API level 16 (current min is 14): android.app.Notification.Builder#build

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

Manifest:

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

Am I missing something?

Correct me if I am wrong but the API is added in level 11, right? Added in API level 11

War es hilfreich?

Lösung

NotificationBuilder.build() requires API Level 16 or higher. Anything between API Level 11 & 15 you should use NotificationBuilder.getNotification(). So use

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

Andere Tipps

The tip about the API level needing to be 16 is right. This worked for me

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

I was having the problem where the notification worked fine on newer devices but not on Android 4.0.4 (API level 15). I do get a deprecation warning form eclipse. @SuppressWarnings("deprecation") doesn't entirely hide it but I think that is probably helpful.

I have same problem in android studio. I did minor change in build.gradle(module:app) file and Problem has been solved. enter image description here

After sync project.

Android Lint is a new tool introduced in ADT 16 (and Tools 16) which scans Android project sources for potential bugs. It is available both as a command line tool, as well as integrated with Eclipse

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

For list of lint checks

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

For supressing lint warning

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

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

If your app supports versions of Android as old as API level 4, you can instead use NotificationCompat.Builder, available in the Android Support library.

For support library

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

You can use from this :

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);
            }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top