在 Android Api 30 中,Intent的putExtra()和getExtra()方法在某些情况下会出现问题。为了解决该问题,你可以采用以下两种方法之一:
方法1:使用Bundle替换putExtra()和getExtra()方法
而不是调用Intent的putExtra()和getExtra()方法,你可以使用Bundle来传递数据。例如:
//写入数据到bundle Bundle bundle = new Bundle(); bundle.putString("key", "value");
//读取数据from bundle String value = bundle.getString("key");
方法2:使用Intent.putExtra()和Intent.getExtras().get() 来替代putExtra()和getExtra()方法
你可以使用Intent的putExtra()方法来将数据添加到Intent中。然后,使用getExtras().get()方法来检索该数据。例如:
// 写入数据到Intent Intent intent = new Intent(this, SomeActivity.class); intent.putExtra("key", "value");
// 检索数据from Intent Bundle bundle = intent.getExtras(); String value = bundle.getString("key");
注意:为了避免所有的不兼容问题,我们建议使用方法1来替代putExtra()和getExtra()方法。
请注意,在未来的Android版本中,putExtra()和getExtra()可能会被修改或删除。因此,你应该尽量避免使用putExtra()和getExtra()方法。尽可能地使用Bundle或基于Bundle的容器。