gpt4 book ai didi

android - IntentFilter 无法接收 Serializable 但适用于 String

转载 作者:行者123 更新时间:2023-11-30 03:32:36 26 4
gpt4 key购买 nike

我有一个像这样的可序列化对象:

public class Main implements Serializable {

private static final long serialVersionUID = 5053412967314724078L;
private ArrayList<Carte> main = new ArrayList<Carte>();


public Main() {}
public Main(Carte[] cartes) {
for (Carte carte:cartes) {
this.main.add(carte);
}
}
public Carte[] getMain() {
return (Carte[]) main.toArray();
}

public String toString() {
String result = "";
for (Carte carte : main) {
result += carte.toString() + " ";
}
return result;
}
}

我将可序列化作为额外的 Intent 发送:

Carte[] cards = ...// Which is initialized elsewhere and been checked, there's no problem with that.
Main mainSerial = new Main(cards);
Intent broadcast = new Intent();
broadcast.setAction("coinchutc.RECUP");
broadcast.putExtra("cards", mainSerial);
Log.d("JoueurAgent", "Sending broadcast " + broadcast.getAction() + " " + mainSerial.toString());
context.sendBroadcast(broadcast);

在Log中我可以正确的打印出debug信息,也就是说我已经成功构造了serializable。

然后我尝试接收 Intent 但没有成功:

在我完成的目标 Activity 的 onCreate() 中:

IntentFilter recupFilter = new IntentFilter();
recupFilter.addAction("coinchutc.RECUP");
registerReceiver(myReceiver, recupFilter);

我已经创建了 BroadcastReceiver 类来接收广播:

private class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.d("RejoindrePartieActivity", "receive");
String action = intent.getAction();
Log.d("RejoindrePartieActivity", "Receive action " + action);
if (action.equalsIgnoreCase("coinchutc.RECUP")) {
Log.d("RejoindrePartieActivity", "RECUP");
Bundle extras = intent.getExtras();
if (extras != null) {
Main main = (Main) extras.getSerializable("cards");
//String recup = extras.getString("cards");
Log.d("RejoindrePartieActivity", "Receive " + main.toString());
}
}
}

但它什么也没收到。连第一条Log都不打印。

然后我将可序列化对象更改为一个简单的字符串。一切都奏效了!所以我想知道我是否得到了可序列化的权利,为什么 intentfilter 甚至无法接收 intent。

提前致谢!

最佳答案

IntentFilter recupFilter = new IntentFilter();
recupFilter.addAction("coinchutc.RECUP");
registerReceiver(myReceiver, recupFilter);

以上代码必须在 sendBroadcast() 之前调用。在发送 Intent 之前调用它。


经过 SO- 的一些澄清后

Carte class also needs to be `Serializable` and this does work. 

关于android - IntentFilter 无法接收 Serializable 但适用于 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17189677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com