Home » Android » android – Why does it say "deprecated" for a method when its the only one I can use for the selected API-level?

android – Why does it say "deprecated" for a method when its the only one I can use for the selected API-level?

Posted by: admin June 15, 2020 Leave a comment

Questions:

In my Android manifest, it says this:

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="16" />

but when I write this code, the getNotification at the end gets me warning saying that the method is “deprecated”:

Notification myNotification = new Notification.Builder(appContext)
.setContentTitle("SIC")
.setContentText(tickerText)
.setWhen(when)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.getNotification(); // <-- warning here

Now, the problem is that for API-level 10, which is the minimum I am developing for, getNotification is the only one there is to use. The newer method called “build()” is for API-level 16.

So why am I getting the deprecated warning even though its the only one I can and should use? One might think that the warning/docs should adapt to the minSdkLevel, not the highets one…

How to&Answers:

So why am I getting the deprecated warning even though its the only one I can and should use?

Because your build target is API Level 16 or higher, where this method is deprecated.

One might think that the warning/docs should adapt to the minSdkLevel, not the highets one

In that case, one would be incorrect. Deprecation has nothing to do with minSdkVersion, any more than it would in standard Java outside of Android (where deprecation exists and minSdkVersion does not).

Moreover, you should be using the Android Support package’s version of Notification.Builder (called NotificationCompat.Builder, since the native one does not exist in API Level 10, which your manifest indicates that you are trying to support. build() should exist on NotificationCompat.Builder and work on all your desired API levels.

Answer:

If you wanted to do it the “technically correct” way, you’d do something like

Notification bldr = new Notification.Builder(appContext)
    .setContentTitle("SIC")
    .setContentText(tickerText)
    .setWhen(when)
    .setDefaults(Notification.DEFAULT_SOUND)
    .setAutoCancel(true)
    .setContentIntent(contentIntent);

Notification notif;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
    notif = bldr.getNotification() 
} else {
    notif = bldr.build();
}

And then annotate the method with TargetApi(16). This is how you generally approach these types of problems.

In your case, you should just use NotificationCompat.Builder instead in the compatibility package.

Answer:

This is an example how to create a notification using NotificationCompat class, supported in all API´s

public static void createNotificacion(Context ctx) {
    try {
        int smallIcon = R.drawable.ic_launcher;
        Bitmap largeIcon = BitmapFactory.decodeResource(ctx.getResources(),R.drawable.ic_launcher);
        long when = System.currentTimeMillis();
        CharSequence contentTitle = "Jorgesys";
        CharSequence contentText = "Hello Stackoverflow!!!";
        Intent notificationIntent = new Intent();
        /*create new task for each notification with pending intent so we set Intent.FLAG_ACTIVITY_NEW_TASK */
        PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
        /*get the system service that manage notification NotificationManager*/
        NotificationManager notificationManager =(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); 
        /*build the notification*/
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
        .setWhen(when)
        .setContentText(contentText)
        .setContentTitle(contentTitle)
        .setSmallIcon(smallIcon)
        .setAutoCancel(true)
        .setTicker(contentTitle)
        .setLargeIcon(largeIcon)
        .setContentIntent(pendingIntent);
        /*sending notification to system. Here we use unique id (when)for making different each notification
         * if we use same id, then first notification replace by the last notification*/
        notificationManager.notify((int) when, notificationBuilder.build()); 
    } catch (Exception e) {
                Log.e("Notification Exception", e.getMessage());
    }
}

Answer:

best thing to do is to use the compatability library and forget about this problem:

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

this way , you can enjoy all of the latest features of the notifications …

Answer:

I believe the warning relates to the fact that you’re building with a newer SDK (due to your targetSDKVersion being higher). So you’re actually able to use the newer method, but you need to be careful when using it, since devices that are below the target API level but above the min API level will not work with the newer API. You can check the API level before using the newer calls though to use them safely.

For example, if you wanted to use an Android 4.1 method while your minSDK is 2.3, you could do:

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
    // API Specific code here
}

In this case the warning doesn’t matter. You can add a @SuppressWarnings(“deprecation”) to the class or method.