FreePhoenix888
FreePhoenix888

Reputation: 6087

How to handle `is deprecated. Deprecated in Java`?

The code I want to use:

window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)

There is FLAG_SHOW_WHEN_LOCKED that is deprecated in the API 27 and its alternative setShowWhenLocked added in the API 27 How should I use it properly if the minSdk in my project is 21 and the targetSdk is 33?

I get the warning is deprecated. Deprecated in Java
Even if I handle it this way:

if(Build.VERSION.SDK_INT >= 27) {
    setShowWhenLocked(true)
    setTurnScreenOn(true)
} else {
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
}

I still get the warning. What is the right way to support both old and new API?

Upvotes: 5

Views: 3197

Answers (1)

FreePhoenix888
FreePhoenix888

Reputation: 6087

TLDR

1.Use different code for different API versions.
2.Ignore/surpress this warning if you properly proccess all the API versions that your app is created for
3.If there is a new alternative that works for all the API levels - use it

Example with the FLAG_SHOW_WHEN_LOCKED/setShowWhenLocked

  1. Use Build.VERSION.SDK_INT in the condition to behave accordingly to the SDK_INT
  2. Use setshowwhenlocked if SDK_INT>=27 and FLAG_SHOW_WHEN_LOCKED if SDK_INT<27
  3. Surpress the warning
if(Build.VERSION.SDK_INT >= 27) {
    setShowWhenLocked(true)
    setTurnScreenOn(true)
} else {
    @Suppress("DEPRECATION")
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
}

But why do we have to surpsess the warning?

The warning exists only because @Deprecated APIs do not have any metadata that would indicate which SDK they were deprecated in. as you can see in this issue. We can surpress the error because we have properly processed both old api (5-27) and new api (27>)

Warning

Do not surpress these warnings if the code is not properly processed by using if conditions where the right API is used.

Example how you must not do

@Suppress("DEPRECATION")
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)

Your minSdk is 21 and targetSdk is 33 This code will work on devices with 5-27 API (Android 5 - Android 8.1) but will not work on new devices. You must properly handle both conditions.

Example with the Vibrator

The old way to get the vibrator

context.getSystemService(VIBRATOR_SERVICE) as Vibrator

The new way to get the vibrator

val vibrator = if (Build.VERSION.SDK_INT >= 31) {
    val vibratorManager =
        context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
    vibratorManager.defaultVibrator
} else {
    @Suppress("DEPRECATION")
    context.getSystemService(VIBRATOR_SERVICE) as Vibrator
}
Will show you the warning `'VIBRATOR_SERVICE: String' is deprecated. Deprecated in Java`. Go to the [documentation](https://developer.android.com/reference/kotlin/android/content/Context#vibrator_service) and see that this constant can be used in the API 1-31 so we must. And in both IDE and documentation there is the info about the alternative: `Deprecated: Use android.os.VibratorManager to retrieve the default system vibrator.`. As you can see the [VibrationManager](https://developer.android.com/reference/kotlin/android/os/VibratorManager) is added in the API 31 therefore we must write the different code for different sdk versions

If an alternative is backwards compatible

If an alternative is backwards compatible you can just use it instead of the old way

Example

If you inherit AppCompatActivity in your activity:

class SaveMyLifeActivity : AppCompatActivity()

You can meet the warning startActivityForResult(Intent!, Int): Unit' is deprecated. Deprecated in Java if you call startActivityForResult:

val intent = Intent(this, SaveMyLifeActivity::class.java)
startActivityForResult(intent, 0)

You can press Alt+Q (Default keybind) to see the Context info(it is called this way in the AndroidStudio is you check your keymap) or use the website do see the documentation Context info Take a look to the words that this method is deprecated and you must use registerForActivityResult instead. This method can be called in any version right now, there are no Added/Deprecated "section" in the documentation.

Question: How have you found this documentation? I google AppCombatActivity startActivityForResult and come to this documentation. There is no word about startActivityForResult.
Answer: Open the context info about this method in the IDE (Alt+Q) and look at the bottom of the Context info Context info. There is a class name where this method is located in (ComponentActivity). You have to google ComponentActivity startActivityForResult instead of AppCombatActivity startActivityForResult

Upvotes: 8

Related Questions