<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-348  評論-598  文章-0  trackbacks-0

    程序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的發(fā)送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的發(fā)送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);

    如果想讓目標(biāo)Activity或者Service接收到相應(yīng)的Broadcast,就需要完全匹配目標(biāo)Activity或者Service的IntentFilter,否則目標(biāo)Filter無法接收到相應(yīng)的Broadcast。注意:IntentFilter設(shè)置的時候都要默認(rèn)帶上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.

    ---------------------------------------------------------
    專注移動開發(fā)

    Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
    posted on 2010-01-31 19:48 TiGERTiAN 閱讀(4921) 評論(0)  編輯  收藏 所屬分類: JavaAndroid
    主站蜘蛛池模板: 成人免费毛片视频| 国产亚洲欧美在线观看| 亚洲一本大道无码av天堂| 日本片免费观看一区二区| 国产精品高清免费网站| 国产精品久久久久久亚洲影视| 亚洲色图视频在线观看| 亚洲精品高清国产一线久久| 国产一级高清视频免费看| 免费下载成人电影| 69视频在线观看高清免费| 中文字幕免费在线视频| 福利片免费一区二区三区| 亚洲国产视频久久| 亚洲天堂电影在线观看| 亚洲91av视频| 日韩va亚洲va欧洲va国产| 亚洲婷婷国产精品电影人久久| 国产精品免费看久久久久| 成年女人免费视频播放77777 | 成人奭片免费观看| **aaaaa毛片免费| 无码少妇精品一区二区免费动态| 国产一级高青免费| 9久久免费国产精品特黄| 人成电影网在线观看免费| 色婷婷亚洲一区二区三区| 亚洲精品久久无码| 亚洲日韩国产一区二区三区在线| 亚洲乱码在线播放| 亚洲无砖砖区免费| 亚洲午夜在线一区| 亚洲国产精品免费观看| 亚洲人成人网毛片在线播放| 亚洲中文字幕一二三四区苍井空| 亚洲国产精品人久久电影| 亚洲另类春色校园小说| 亚洲精品福利你懂| 亚洲日韩国产AV无码无码精品| 亚洲欧美不卡高清在线| 国产精品亚洲综合天堂夜夜|