首页 > 其他 > 详细

BroadCastReceiver 向Activity传值的问题

时间:2015-05-31 13:36:22      阅读:180      评论:0      收藏:0      [点我收藏+]

我们在有时候在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 类

BroadCastReceiver 向Activity传值的问题

原文:http://www.cnblogs.com/liutanrong/p/4541786.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!