我想使用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)
}
})
}
}
此代码可以让谷歌播放拒绝我的应用程序吗?因为在删除 (SDK_INT >= 33) 语句时它显示 调用需要 API 级别 33(当前最低级别为 21):android.content.Intent#getParcelableExtra。预先感谢
不,如果您使用已弃用的方法,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)
}
这些是
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)
}
}
将 uri.toString() 作为额外的字符串,而不是 uri。
非常简单。
IntentCompat
类,方法如下:
IntentCompat.getParcelableExtra(intent, "EXTRA_URI", Uri::class.java)
为了更方便,您可以创建一个扩展:
inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? =
IntentCompat.getParcelableExtra(this, key, T::class.java)