[英]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的正确方法是什么?
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
级别的新替代方案 - 使用它
Build.VERSION.SDK_INT
in the condition to behave accordingly to the SDK_INT在条件中使用Build.VERSION.SDK_INT
以根据 SDK_INT 进行相应操作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
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)
}
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>)
Do not surpress these warnings if the code is not properly processed by using if conditions where the right API is used.如果使用正确的 API 条件未正确处理代码,请不要隐藏这些警告。
@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.您必须妥善处理这两种情况。
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 you can just use it instead of the old way如果替代方案向后兼容,您可以使用它而不是旧方法
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.