简体   繁体   English

如何处理`已弃用。 在 Java 中弃用?

[英]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? API中有FLAG_SHOW_WHEN_LOCKED被弃用 27 setShowWhenLocked中有替代的setShowWhenLocked 27 如果我的项目中的minSdk是21,targetSdk是33,我应该如何正确使用它?

I get the warning is deprecated. Deprecated in Java我收到警告is deprecated. Deprecated in Java 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?支持新旧API的正确方法是什么?

TLDR TLDR

1.Use different code for different API versions. 1.不同的API版本使用不同的代码。
2.Ignore/surpress this warning if you properly proccess all the API versions that your app is created for 2.如果您正确处理了为您的应用程序创建的所有 API 版本,则忽略/取消此警告
3.If there is a new alternative that works for all the API levels - use it 3.如果有适用于所有API级别的新替代方案 - 使用它

Instruction操作说明

  1. Use Build.VERSION.SDK_INT in the condition to behave accordingly to the SDK_INT在条件中使用Build.VERSION.SDK_INT以根据 SDK_INT 进行相应操作
  2. Use setshowwhenlocked if SDK_INT>=27 and FLAG_SHOW_WHEN_LOCKED if SDK_INT<27如果SDK_INT>=27使用setshowwhenlocked ,如果SDK_INT<27使用FLAG_SHOW_WHEN_LOCKED
  3. Surpress the warning压制警告

Example with the FLAG_SHOW_WHEN_LOCKED/setShowWhenLocked带有FLAG_SHOW_WHEN_LOCKED/setShowWhenLocked的示例

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 .该警告存在的唯一原因是@Deprecated API 没有任何元数据可以指示它们在哪个 SDK 中被弃用。正如您在本期中看到的那样。 We can surpress the error because we have properly processed both old api (5-27) and new api (27>)我们可以抑制错误,因为我们已经正确处理了旧的 api (5-27) 和新的 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.如果使用正确的 API 条件未正确处理代码,请不要隐藏这些警告。

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.您的 minSdk 是 21,targetSdk 是 33 此代码适用于 5-27 API (Android 5 - Android 8.1) 的设备,但不适用于新设备。 You must properly handle both conditions.您必须妥善处理这两种情况。

Example with the Vibrator 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:如果您在活动中继承AppCompatActivity

class SaveMyLifeActivity : AppCompatActivity()

You can meet the warning startActivityForResult(Intent,: Int). Unit' is deprecated. Deprecated in Java可以遇到警告startActivityForResult(Intent,: Int). Unit' is deprecated. Deprecated in Java startActivityForResult(Intent,: Int). Unit' is deprecated. Deprecated in Java startActivityForResult(Intent,: Int). Unit' is deprecated. Deprecated in Java if you call startActivityForResult :如果您调用startActivityForResult ,则startActivityForResult(Intent,: Int). Unit' is deprecated. Deprecated in Java

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您可以按 Alt+Q(默认键绑定)查看Context info (在 AndroidStudio 中以这种方式调用是您检查您的键盘映射)或使用网站查看文档上下文信息 Take a look to the words that this method is deprecated and you must use registerForActivityResult instead.看看这个方法被弃用的词,你必须使用registerForActivityResult代替。 This method can be called in any version right now, there are no Added/Deprecated "section" in the documentation.现在可以在任何版本中调用此方法,文档中没有Added/Deprecated的“部分”。

Question: How have you found this documentation?问题:您是如何找到该文档的? I google AppCombatActivity startActivityForResult and come to this documentation.我用谷歌搜索AppCombatActivity startActivityForResult并找到文档。 There is no word about startActivityForResult .没有关于startActivityForResult的消息。
Answer: Open the context info about this method in the IDE (Alt+Q) and look at the bottom of the Context info答:在IDE(Alt+Q)中打开此方法的上下文信息,查看Context info的底部上下文信息 . . There is a class name where this method is located in ( ComponentActivity ).这个方法位于( ComponentActivity )中有一个 class 名称。 You have to google ComponentActivity startActivityForResult instead of AppCombatActivity startActivityForResult你必须谷歌ComponentActivity startActivityForResult而不是AppCombatActivity startActivityForResult

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 默认显示的吸气剂:显示。 已弃用。 在 Java 中已弃用 - Getter for defaultDisplay: Display!' is deprecated. Deprecated in Java 'setHasOptionsMenu(Boolean): Unit' 已弃用。 在 Java 中已弃用 - 'setHasOptionsMenu(Boolean): Unit' is deprecated. Deprecated in Java 不推荐使用“ViewModelProviders”。 Java解决方案? - 'ViewModelProviders' is deprecated. Java solution? '签名:数组&lt;(out)签名。&gt;!' 已弃用。 在 Java 中已弃用 - 'signatures: Array<(out) Signature!>!' is deprecated. Deprecated in Java 'onPrepareOptionsPanel(View?, Menu): Boolean' 已弃用。 在 Java 中已弃用 - 'onPrepareOptionsPanel(View?, Menu): Boolean' is deprecated. Deprecated in Java 不推荐使用StreamType。 如何获取流类型? - StreamType deprecated. How to get the stream type? 不推荐使用AppInviteReferral。 还有其他选择吗? - AppInviteReferral is deprecated. Is there an alternative? WifiManager startScan 已弃用。 选择? - WifiManager startScan deprecated. Alternative? isMinifyEnabled()已弃用。 有什么选择? - isMinifyEnabled() is deprecated. What is the alternative? getDrawable(int id)已过时。 如何设置图像? - getDrawable(int id) is deprecated. How can I set an image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM