Suppress API level warning in code

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() {}

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.


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> ... }