Broadcastreceiver in Activity

时间:2014-09-17 13:42:51

标签: android xamarin xamarin.android

在我的服务中,我有一个SendBroadCast(),它使用我需要的信息发送我的意图(用于活动中的更新列表视图)。但是如何从活动中检索此信息?如果我为BroadcastReceiver创建一个扩展类,我可以检索这些信息,但是如何进行活动?

我的服务:

            Intent myIn = new Intent(this, typeof(MyActivity));

            Bundle bundle = new Bundle();

            bundle.PutString("value1", "value_1");

            bundle.PutString("value2", "value_2");

            myIn.PutExtra("myaction",bundle);

            SendBroadcast(myIn);

2 个答案:

答案 0 :(得分:0)

u can send like this,


 Intent myIn = new Intent(this, typeof(MyActivity));

         //instead of bundle use like this 

            myIn.PutExtra("value1", "value_1");

            myIn.PutExtra("value2", "value_2");

          SendBroadcast(myIn);


and get them on activity like,


      private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            String value1 = intent.getStringExtra("value1");
      String value2 = intent.getStringExtra("value2");


        }

    };

答案 1 :(得分:0)

您可以在下一个活动中获得数据......

String  abc, Last;
Bundle extras = getIntent().getExtras(); 
Last = extras.getString("value1");
abc = extras.getString("value2");