getParcelableExtra 方法已弃用

问题描述 投票:0回答:5

我将 Parcelable 数据传递到 Intent 中,并使用 getParcelableExtra(name:) 方法将其从另一端取出。然而, getParcelableExtra(name:) 似乎已被弃用。如何修复弃用警告?或者,还有其他选择吗?我使用的

compileSdkVersion
值为 33。

代码片段

var data = intent.getParcelableExtra("data")
android android-intent parcelable
5个回答
165
投票

这是我用于

Bundle
Intent
的两种扩展方法:

inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? = when {
  SDK_INT >= 33 -> getParcelableExtra(key, T::class.java)
  else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T
}

inline fun <reified T : Parcelable> Bundle.parcelable(key: String): T? = when {
  SDK_INT >= 33 -> getParcelable(key, T::class.java)
  else -> @Suppress("DEPRECATION") getParcelable(key) as? T
}

我还请求将其添加到支持库中

如果您需要 ArrayList 支持:

inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String): ArrayList<T>? = when {
  SDK_INT >= 33 -> getParcelableArrayList(key, T::class.java)
  else -> @Suppress("DEPRECATION") getParcelableArrayList(key)
}

inline fun <reified T : Parcelable> Intent.parcelableArrayList(key: String): ArrayList<T>? = when {
  SDK_INT >= 33 -> getParcelableArrayListExtra(key, T::class.java)
  else -> @Suppress("DEPRECATION") getParcelableArrayListExtra(key)
}

注意:新方法在 SDK 33 上存在一些问题,因此您可能只想从 SDK 34 开始使用它。


34
投票
现在我们需要使用

getParcelableExtra() 以及添加到 API 33 的类型安全类


kotlin 的示例代码

val userData = if (VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { intent.getParcelableExtra("DATA", User::class.java) } else { intent.getParcelableExtra<User>("DATA") }

JAVA 示例代码

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { user = getIntent().getParcelableExtra("data", User.class); } else { user = getIntent().getParcelableExtra("data"); }



20
投票
androidx.core:core-ktx:1.10.0

提供了两个类IntentCompat

BundleCompat
以及一些有用的静态方法

androidx.core.os.BundleCompat

BundleCompat.getParcelable(Bundle, String, Class<T>)
BundleCompat.getParcelableArray(Bundle, String, Class<? extends Parcelable>): Parcelable[] 
BundleCompat.getParcelableArrayList(Bundle, String, Class<? extends T> clazz): ArrayList<T>
BundleCompat.getSparseParcelableArray(Bundle, String, Class<? extends T>): SparseArray<T>

androidx.core.content.IntentCompat

IntentCompat.getParcelableExtra(Intent, String, Class<T>)
IntentCompat.getParcelableArrayExtra(Intent, String, Class<? extends Parcelable>): Parcelable[]
IntentCompat.getParcelableArrayListExtra(Intent, String, Class<? extends T>): ArrayList<T>

检索 
android.bluetooth.BluetoothDevice

对象的示例:

IntentCompat.getParcelableExtra(intent, BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java) 



7
投票
官方文档

中所述,getParcelableExtra在API级别33中已弃用。

所以检查 API LEVEL 是否 >= 33 或更改方法,

... if (Build.VERSION.SDK_INT >= 33) { // TIRAMISU data = intent.getParcelableExtra (String name, Class<T> clazz) }else{ data = intent.getParcelableExtra("") }

这是一个使用 android.bluetooth.BluetoothDevice 的示例

... val device = if (Build.VERSION.SDK_INT >= 33){ // TIRAMISU intent.getParcelableArrayExtra( BluetoothDevice.EXTRA_NAME, BluetoothDevice::class.java ) }else{ intent.getParcelableExtra(BluetoothDevice.EXTRA_NAME) }



6
投票

UsbDevice device; if (Build.VERSION.SDK_INT > Build.VERSION_CODES.S_V2) { // TIRAMISU onwards device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE, UsbDevice.class); } else { device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); }

这还需要
@SuppressWarnings({"deprecation", "RedundantSuppression"})

    

© www.soinside.com 2019 - 2024. All rights reserved.