关闭

[转载]Android中用广播从Service中向Activity发送信息

809人阅读 评论(0) 收藏 举报

http://blog.sina.com.cn/s/blog_61cd1c5f0100s7bv.html


1.在Service中发送广播
 

           Intent intent = newIntent(BroadcastAction);
             Bundlebundle = new Bundle();
            bundle.putString("***", SUCCESS);
            bundle.putString("FullPathName", mFullPathName);
            intent.putExtras(bundle);
            sendBroadcast(intent);
2.在Activity中创建一个内部类MyBroadcastReceiver扩展BroadcastReceiver,并在其中实现onReceive方法。
3.在Activity中声明一个MyBroadcastReceiver类型的成员变量,并注册:
    private MyBroadcastReceiver myBroadcastReceiver;
      ...
   myBroadcastReceiver = newMyBroadcastReceiver();
       IntentFilterfilter = new IntentFilter();
      filter.addAction(BroadcastAction);
      registerReceiver(receiver, filter);
4.使用完后要记得释放
      unregisterReceiver(receiver);

注:1和2中的 BroadcastAction要是同一个Action


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:282016次
    • 积分:3403
    • 等级:
    • 排名:第9808名
    • 原创:7篇
    • 转载:430篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论