【问题标题】:Android getParcelableExtra deprecated in api 33Android getParcelableExtra 在 api 33 中被弃用
【发布时间】:2023-01-25 23:38:08
【问题描述】:
我想使用 intent 方法从另一个活动中获取 uri,但不推荐使用 intent.getParcelableExtra。如果我使用
if (SDK_INT >= 33) {
intent.getParcelableExtra("EXTRA_URI", Uri::class.java).let { ueray ->
timeLineView.post({
if (ueray != null) {
setBitmap(ueray)
videoView.setVideoURI(ueray)
}
})
}
}
else {
@Suppress("DEPRECATION")
intent.getParcelableExtra<Uri>("EXTRA_URI").let { ueray ->
timeLineView.post({
if (ueray != null) {
setBitmap(ueray)
videoView.setVideoURI(ueray)
}
})
}
}
此代码可以谷歌播放拒绝我的应用程序吗?因为在 remove (SDK_INT >= 33) 语句中显示
调用需要 API 级别 33(当前最低为 21):android.content.Intent#getParcelableExtra。提前致谢
【问题讨论】:
标签:
android
kotlin
google-play
deprecated
parcelable
【解决方案1】:
这些是 Intent 的扩展函数,它们是向后兼容的:
@Suppress("DEPRECATION")
inline fun <reified P : Parcelable> Intent.getParcelable(key: String): P? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelableExtra(key, P::class.java)
} else {
getParcelableExtra(key)
}
}
@Suppress("DEPRECATION")
inline fun <reified P : Parcelable> Intent.getParcelableArrayList(key: String): ArrayList<P>? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelableArrayListExtra(key, P::class.java)
} else {
getParcelableArrayListExtra(key)
}
}
@Suppress("DEPRECATION")
inline fun <reified P : Parcelable> Bundle.getParcelableValue(key: String): P? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelable(key, P::class.java)
} else {
getParcelable(key)
}
}
@Suppress("DEPRECATION")
inline fun <reified P : Parcelable> Bundle.getParcelableArrayListValue(key: String): ArrayList<P>? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getParcelableArrayList(key, P::class.java)
} else {
getParcelableArrayList(key)
}
}
【解决方案2】:
不,Google 不会拒绝您的应用如果您使用已弃用的方法,尤其是在必须使用它时,因为您别无选择,只能在 SDK < 33 上使用它。
我的应用程序在较低的 SDK 上使用已弃用的方法,这是唯一的可能性,并且该应用程序很好并且可以在 Google Play 商店中访问:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val vibrationEffect = VibrationEffect.createWaveform(
longArrayOf(1000, 1000),
intArrayOf(255, 0),
0
)
vibrator.vibrate(vibrationEffect, vibrationAudioAttributes)
} else {
// deprecated but working on lower SDK's
vibrator.vibrate(longArrayOf(0, 1000, 1000), 0, vibrationAudioAttributes)
}
【解决方案3】:
而不是 uri 把 uri.toString() 作为一个额外的字符串。
非常简单。