簡體   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