Android中用广播从Service中向Activity发送信息

http://blog.sina.com.cn/s/blog_773cd3e30100wzt7.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


你可能感兴趣的:(android,filter,service,扩展,action)