我一直在尝试使用Intent在两个活动之间传递多个(不同类型的)值。到目前为止,我已尝试过这两种方法:
Intent intent = new Intent(context, Receiver.class);
Bundle bundle = new Bundle();
bundle.putInt("key1", v1);
bundle.putString("key2", v2);
bundle.putString("key3", v3);
bundle.putInt("key4", v4);
intent.putExtras(bundle);
和
Intent intent = new Intent(context, Receiver.class);
intent.putInt("key1", v1);
intent.putString("key2", v2);
intent.putString("key3", v3);
intent.putInt("key4", v4);
然而,在两种情况下似乎只保留key1
的值(当使用Bundle时,它显然只包含1个键)。我错过了什么?
修改:这是我在Receiver
中检索值的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
v1 = bundle.getInt("key1", DEFAULT1);
v2 = bundle.getString("key2", "DEFAULT2");
v3 = bundle.getString("key3", "DEFAULT3");
v4 = bundle.getInt("key4", DEFAULT4);
// ...
}
可替换地:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
Intent intent = getIntent();
v1 = intent.getIntExtra("key1", DEFAULT1);
v2 = intent.getStringExtra("key2");
v3 = intent.getStringExtra("key3");
v4 = intent.getIntExtra("key4", DEFAULT2);
// ...
}
当我打印出v1
,v2
,v3
和v4
的值时,v1
是唯一一个非空/ {非默认值(我最初放在key1
中的值 - 实际上,我最初放入Intent的所有值都是非默认值。)
编辑2:
我尝试使用getBundleExtra()
,如此:
intent.putExtra("bundle", bundle);
然后在Receiver
:
Bundle bundle = intent.getBundleExtra("bundle");
但是,bundle
为空。
编辑3:
更改从Intent放入/检索值的顺序似乎不会影响任何内容。也没有指定Bundle的容量。如果有帮助,则此Intent用于PendingIntent。
答案 0 :(得分:0)
您可以在不使用Bundle的情况下简单地传递和接收Intent Extras。
<强> SenderClass.java 强>
>>> Creating reader: 0
>>> Creating reader: 1
>>> Creating reader: 2
>>> Creating writer: 0
INFO::About to access entry mutex for reader with ID: 1, with queue length 1.
INFO::About to exit entry mutex for reader with ID: 1, with queue length 0.
[READER (ID: 1):] Readers in queue: 0, read: 0
>>> Creating writer: 2
>>> Creating writer: 1
[WRITER (ID: 0):] Readers in queue: 0, wrote: 0
[WRITER (ID: 0):] Readers in queue: 0, wrote: 1
!!! Destroying writer: 0
[WRITER (ID: 1):] Readers in queue: 0, wrote: 5
[WRITER (ID: 1):] Readers in queue: 0, wrote: 6
!!! Destroying writer: 1
[WRITER (ID: 2):] Readers in queue: 0, wrote: 10
[WRITER (ID: 2):] Readers in queue: 0, wrote: 11
!!! Destroying writer: 2
INFO::About to access entry mutex for reader with ID: 1, with queue length 1.
INFO::About to exit entry mutex for reader with ID: 1, with queue length 0.
[READER (ID: 1):] Readers in queue: 0, read: 11
!!! Destroying reader: 1
INFO::About to access entry mutex for reader with ID: 2, with queue length 1.
INFO::About to exit entry mutex for reader with ID: 2, with queue length 0.
[READER (ID: 2):] Readers in queue: 0, read: 11
INFO::About to access entry mutex for reader with ID: 0, with queue length 1.
INFO::About to exit entry mutex for reader with ID: 0, with queue length 0.
[READER (ID: 0):] Readers in queue: 0, read: 11
INFO::About to access entry mutex for reader with ID: 0, with queue length 1.
INFO::About to exit entry mutex for reader with ID: 0, with queue length 0.
[READER (ID: 0):] Readers in queue: 0, read: 11
!!! Destroying reader: 0
INFO::About to access entry mutex for reader with ID: 2, with queue length 1.
ERROR::Before entry mutex!
ERROR::During entry mutex!
INFO::About to exit entry mutex for reader with ID: 2, with queue length -1.
[READER (ID: 2):] Readers in queue: -1, read: 11
!!! Destroying reader: 2
<强> Receiver.java 强>
Intent intent = new Intent(getApplicationContext(), Receiver.class);
intent.putExtra("key1", key1);
intent.putExtra("key2", key2);
intent.putExtra("key3", key3);
startActivity(intent)
答案 1 :(得分:0)
原来我忽略了一些非常简单的东西 - 我的意图在PendingIntent中被用来触发接收器然后启动服务,但我忘了将Bundle从原来的Intent正确地转移到(新的,不同的)Intent最终由第二个活动(由服务启动)收到。问题已经解决了。