gpt4 book ai didi

java - 从 BroadcastReceiver 获取主 Activity 数据

转载 作者:行者123 更新时间:2023-12-02 04:46:19 25 4
gpt4 key购买 nike

我读了很多类似的问题,但我不知道如何做到这一点。我的 MainActivity 包含以下代码:

  MyBroadcastReceiver broad = new MyBroadcastReceiver();
broad.onReceive(context, new Intent("api6.intent.action.TRANSACTION_COMPLETED"));

然后在 MyBroadcastReceiver 中,除了主类之外,我有:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intents.ACTION_TRANSACTION_COMPLETED.equals(action)) {
if (intent.getExtras() != null) {
String txid = String.valueOf(intent.getExtras().get(Intents.INTENT_EXTRAS_TRANSACTION_ID));
}
}
}
}

我需要 MainActivity 中此行之后的字符串 txid:

         broad.onReceive(context, new Intent("api6.intent.action.TRANSACTION_COMPLETED"));

最佳答案

您可以通过多种方式实现,可以通过在 MainActivity 类中编写广播类,也可以使用接口(interface),在 MainActivity 中实现它

例如

MA 扩展 AppCompat 实现 AnInterface{

onCreate(){

broadcast = new YourBroadcastClass(this);
}

@override
public void interfaceMethod(String value){
}

以及广播类构造函数内

> AndInterface interface; MyBroad(AnInterface interface){ this.interface
> = interface; }
>
> and onReceive(){ interface.interfaceMethod("whatever you wanna send")

关于java - 从 BroadcastReceiver 获取主 Activity 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56480653/

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