【发布时间】:2018-03-18 21:12:46
【问题描述】:
我有 Activity A,它使用以下代码启动 Activity B:
Intent intent = new Intent(this, B.class);
intent.putExtra("foo", new MySerializableObject());
startActivity(intent);
在 B 中“foo”被正确接收,然后我创建 PendingIntent 以在一段时间后自行启动,您可以将其视为一些闹钟应用程序。无论如何,神秘的是,当我按以下方式安排此意图时:
Intent intent = new Intent(context, B.class);
intent.putExtra("bar", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + delayMs, pendingIntent);
然后一切都很好(在收到此意图后“bar”值为真),但是如果我在“bar”之前或之后添加以下行:
intent.putExtra("foo", new MySerializableObject());
然后,当我收到此意图时,“foo”和“bar”都不见了。我的意思是这两行都返回了 false:
getIntent().hasExtra("foo")
getIntent().hasExtra("bar")
这种行为的原因可能是什么?
编辑: 根据我尝试过的 cmets 中的建议:
intent.putExtra("foo", true);
intent.putExtra("bar", true);
它起作用了,所以我认为MySerializableObject可能有问题,所以这是我接下来尝试的:
intent.putExtra("foo",
new Serializable() {
@Override
public int hashCode() { return super.hashCode(); }
});
intent.putExtra("bar", true);
但这会导致与我描述的完全相同的问题(“foo”和“bar”)丢失。最后我也尝试用“xxx”替换“foo”,但它没有改变任何东西,所以对我来说它看起来像是一些奇怪的 Android 错误。
【问题讨论】:
-
你试过用 Bundle.putSerializable()
-
不,但 putExtra 本身没有任何问题。请注意,当从 A 开始 B 时,我使用完全相同的行并且它工作正常(收到“foo”)。
-
你可以试试
intent.putExtra("foo", true);并检查你是否得到了这个值? -
检查this是否有帮助
-
你能查一下吗?
标签: android android-intent android-pendingintent