繁体   English   中英

从BroadcastReceiver获取主活动数据

[英]Get in the Main Activity data from a BroadcastReceiver

我提到了许多类似的问题,但我无法弄清楚如何做到这一点。 我有这个代码的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中使用String txid:

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

您可以通过在MainActivity类中编写广播类或使用接口在很多方面实现,在MainActivity中实现它

例如

MA extends AppCompat implements 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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM