Android的Broadcast应用说明一例

程序A的Receiver:

receiver  =   new  BroadcastReceiver()  {

            @Override
            
public void onReceive(Context ctx, Intent intent) {
                Log.i(
"TestActivity""===============Broadcast Received:" + intent.getAction());
                
if (intent.getAction().equals(RESPONSE_ACTION)) {
                    Uri uri 
= intent.getData();
                    String location 
= "";
                    
if (uri != null{
                        Log.i(
"TestActivity",
                                
"=======================parameter location:"
                                        
+ uri.getQueryParameter("location"));
                        Log.i(
"TestActivity""======================= host:"
                                
+ uri.getHost());
                        Log.i(
"TestActivity""======================= path:"
                                
+ uri.getPath());
                        location 
= uri.getQueryParameter("location");
                        FileInputStream fis 
= null;
                        ByteArrayOutputStream bos 
= null;
                        File file 
= new File(location);
                        
try {
                            fis 
= new FileInputStream(file);
                            bos 
= new ByteArrayOutputStream();
                            
byte[] data = new byte[512];
                            
int count = -1;
                            
while ((count = fis.read(data)) != -1{
                                bos.write(data, 
0, count);
                            }

                            String xml 
= new String(bos.toByteArray());
                            Log.i(
"TestActivity""xml1=" + xml);
                            Toast.makeText(TestActivity.
this, xml, Toast.LENGTH_SHORT).show();
                        }
 catch (Exception ex) {
                            ex.printStackTrace();
                        }
 finally {
                            
if (fis != null)
                                
try {
                                    fis.close();
                                }
 catch (IOException e) {
                                    
// TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                            
if (bos != null)
                                
try {
                                    bos.close();
                                }
 catch (IOException e) {
                                    
// TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                        }

                    }

                }

            }


        }
;
        IntentFilter filter 
=   new  IntentFilter();
        filter.addAction(RESPONSE_ACTION);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        filter.addDataScheme(
"http " );
        registerReceiver(receiver, filter);
程序A的发送BC:
        btn.setOnClickListener( new  OnClickListener()  {

            @Override
            
public void onClick(View arg0) {
                
// TODO Auto-generated method stub
                Uri uri = Uri.parse("http://www.xx.com/getDetails?id=123");
                Intent in 
= new Intent();
                in.setAction(REQUEST_ACTION);
                in.addCategory(Intent.CATEGORY_DEFAULT);
                in.setData(uri);
                TestActivity.
this.sendBroadcast(in);
            }

        }
);


程序B的Receiver:
@Override
    
public   void  onReceive(Context context, Intent intent)  {
        
if(intent != null)
        
{
            Uri uri 
= intent.getData();
            
if(uri != null)
            
{
                Log.i(
"BGServiceReceiver""=======================" + uri.getQueryParameter("id"));
                intent.setClassName(
"com.backgroundservice",
                
"com.backgroundservice.TestBackgroundService");
                context.startService(intent);
            }

                
        }

//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    

    }
配置:
         < receiver  android:name ="ServiceReceiver" >
            
< intent-filter >
                
< action  android:name ="android.intent.action.STORE_REQUEST" ></ action >
                
< category  android:name ="android.intent.category.DEFAULT" ></ category >
                
< data  android:scheme ="http" ></ data >
            
</ intent-filter >
        
</ receiver >

程序B的发送BC:
        Intent in  =   new  Intent();
        in.setAction(RESPONSE_ACTION);
        in.addCategory(Intent.CATEGORY_DEFAULT);
        in.setData(Uri.parse(
" http://www.xx.com/setList?location=/sdcard/xx.xml " ));
        
this .sendBroadcast(in);


如果想让目标Activity或者Service接收到相应的Broadcast,就需要完全匹配目标Activity或者Service的IntentFilter,否则目标Filter无法接收到相应的Broadcast。注意:IntentFilter设置的时候都要默认带上CATEGORY_DEFAULT,否则可能会造成潜在问题。
文档中的说明如下:
In principle, therefore, an Intent object with no categories should always pass this test, regardless of what's in the filter. That's mostly true. However, with one exception, Android treats all implicit intents passed to startActivity() as if they contained at least one category: "android.intent.category.DEFAULT" (the CATEGORY_DEFAULT constant). Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters.

 

TrackBack:http://www.blogjava.net/TiGERTiAN/archive/2010/01/31/311413.html

转载于:https://www.cnblogs.com/hdjjun/archive/2010/06/02/1749859.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值