Suppress API level warning in code
Asked Answered
M

4

8

I am trying to create an intent to create an sms with compatibility for API levels higher than KitKat. The code works, but I got a warning that the API level 19 is required. I tried to solve it with @TargetApi(Build.VERSION_CODES.KITKAT) but got a warning "Annotations are not allowed here".

Is there an easy way to ignore this warning?

private boolean apLowerThanKitKat = (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT);

Intent smsIntent = new Intent(android.content.Intent.ACTION_SEND);
if (apLowerThanKitKat)
{
    smsIntent.setPackage("vnd.android-dir/mms-sms");
}
else
{
    //@TargetApi(Build.VERSION_CODES.KITKAT)
    smsIntent.setPackage(Telephony.Sms.getDefaultSmsPackage(getActivity()));
}

Thanks in advance!

Meingoldas answered 30/12, 2015 at 12:53 Comment(0)
P
12

Don't use a boolean for the API checking. Put it directly in the if statement:

if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT)
{
    smsIntent.setPackage("vnd.android-dir/mms-sms");
}
else
{
    smsIntent.setPackage(Telephony.Sms.getDefaultSmsPackage(getActivity()));
}

That should make the warning go away. TargetApi annotations should be done at the method level:

@TargetApi(Build.VERSION_CODES.KITKAT)
public void yourKitkatMethod() {}
Perutz answered 30/12, 2015 at 12:59 Comment(3)
Tried to be smart and improve readability by using the boolean :p But this works perfectly, thanks!Meingoldas
@Meingoldas been there :)Perutz
TargetApi can also be used at class levelUam
O
4

Annotation needs to be on a method

private boolean apLowerThanKitKat = (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT);

Intent smsIntent = new Intent(android.content.Intent.ACTION_SEND);
if (apLowerThanKitKat)
{
    smsIntent.setPackage("vnd.android-dir/mms-sms");
}
else
{
    kitkatSetPackage():
}

@TargetApi(Build.VERSION_CODES.KITKAT)
private void kitkatSetPackage() {
    smsIntent.setPackage(Telephony.Sms.getDefaultSmsPackage(getActivity()));
}

You could move this annotation to other methods (if all of this code is in one method), or put the annotation on the class it self.

Orrery answered 30/12, 2015 at 12:59 Comment(1)
This answer works great as well, but the accepted answer is a bit faster to implement.Meingoldas
T
4

I'm constantly moving from version to version in the AOSP, and I don't want to mislead the next person reading my code with a specific @TargetApi() version annotation. So you can do the following instead:

@SuppressLint("NewApi")
public void xyzMethod() { <your_code> ... }
Tarsier answered 22/1, 2019 at 19:49 Comment(1)
In many cases, you can make the API warning go away by fixing your code (adding proper api checks, etc...). However, in some cases, you have to just suppress the warning. This is the answer I was looking for.Quadruplicate
C
1

**Put your code in a method **

@TargetApi(Build.VERSION_CODES.KITKAT)
public void xyzMethod(){
  your code here 
}

that will work now

Cadell answered 30/12, 2015 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.