Fre*_*888 5 android kotlin deprecation-warning
我想使用的代码:
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
Run Code Online (Sandbox Code Playgroud)
FLAG_SHOW_WHEN_LOCKEDAPI 27 中已弃用,并且API setShowWhenLocked27 中添加了替代方案 如果minSdk我的项目中的 是 21 并且 targetSdk 是 33,我应该如何正确使用它?
is deprecated. Deprecated in Java
即使我这样处理,我也会收到警告:
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)
}
Run Code Online (Sandbox Code Playgroud)
我仍然收到警告。同时支持新旧 API 的正确方法是什么?
1.不同的API版本使用不同的代码。
2.如果您正确处理了为您的应用程序创建的所有 API 版本,请忽略/抑制此警告
3.如果有适用于所有API级别的新替代方案 - 请使用它
FLAG_SHOW_WHEN_LOCKED/setShowWhenLockedBuild.VERSION.SDK_INT以根据 SDK_INT 进行操作setshowwhenlockedifSDK_INT>=27和FLAG_SHOW_WHEN_LOCKEDifSDK_INT<27if(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)
}
Run Code Online (Sandbox Code Playgroud)
该警告之所以存在,只是因为@DeprecatedAPI 没有任何元数据来指示它们在哪个 SDK 中被弃用。正如您在本期中看到的那样。我们可以抑制错误,因为我们已经正确处理了旧 api (5-27) 和新 api (27>)
如果使用正确 API 的 if 条件未正确处理代码,请不要抑制这些警告。
@Suppress("DEPRECATION")
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
Run Code Online (Sandbox Code Playgroud)
您的 minSdk 为 21,targetSdk 为 33 此代码适用于具有 5-27 API (Android 5 - Android 8.1) 的设备,但不适用于新设备。您必须妥善处理这两种情况。
Vibrator获取振动器的旧方法
context.getSystemService(VIBRATOR_SERVICE) as Vibrator
Run Code Online (Sandbox Code Playgroud)
获取振动器的新方法
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
Run Code Online (Sandbox Code Playgroud)
如果替代方案向后兼容,您可以使用它而不是旧的方式
AppCompatActivity如果您在活动中继承:
class SaveMyLifeActivity : AppCompatActivity()
Run Code Online (Sandbox Code Playgroud)
startActivityForResult(Intent!, Int): Unit' is deprecated. Deprecated in Java如果您拨打以下电话,您可能会遇到警告startActivityForResult:
val intent = Intent(this, SaveMyLifeActivity::class.java)
startActivityForResult(intent, 0)
Run Code Online (Sandbox Code Playgroud)
您可以按 Alt+Q(默认键绑定)查看Context info(在 AndroidStudio 中称为这种方式,您检查键盘映射)或使用网站查看文档
查看此方法已被弃用且必须使用的字样registerForActivityResult。Added/Deprecated现在可以在任何版本中调用此方法,文档中没有“部分”。
问题:您是如何找到此文档的?我用谷歌搜索AppCombatActivity startActivityForResult并找到了这个文档。没有任何关于 的说法startActivityForResult。
答案:在 IDE 中打开有关此方法的上下文信息(Alt+Q),然后查看底部的Context info
。该方法所在的类名位于 ( ComponentActivity) 中。你必须谷歌ComponentActivity startActivityForResult而不是AppCombatActivity startActivityForResult
| 归档时间: |
|
| 查看次数: |
2048 次 |
| 最近记录: |