BroadCastReceiver 向Activity传值的问题

我们在有时候在BroadCastReceiver中获取的值,但是activity中要用。

Intent in=new Intent(context,guangbo.class);
in.putExtra("msg",msg);
context.startActivity(in);

用以上方法传值,总是会引起报错。我遇到这个找了好多资料,发现可以通过引入一个Bundle来解决。代码如下:

BroadCastReceiver中发送代码

Intent in=new Intent(context,guangbo.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle msgbundle=new Bundle();
msgbundle.putString("address",address);
msgbundle.putString("body",body);
in.putExtras(msgbundle);
context.startActivity(in);

 activity中接收代码

        Bundle bundle = getIntent().getExtras();
        String address = bundle.getString("address");//读出数据
        String body=bundle.getString("body");
        sms=bundle.getString("body");            

 这样就不会报错了。

参考:http://bbs.itcast.cn/thread-15720-1-1.html

      Android Bundle 类

posted @ 2015-05-31 12:21  刘探荣  阅读(1008)  评论(0编辑  收藏  举报