<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的發送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.

    ---------------------------------------------------------
    專注移動開發

    Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
    posted on 2010-01-31 19:48 TiGERTiAN 閱讀(4933) 評論(0)  編輯  收藏 所屬分類: JavaAndroid
    主站蜘蛛池模板: 国产中文在线亚洲精品官网| 久久久久久精品免费免费自慰| 成年女人午夜毛片免费看| 亚洲精品91在线| 免费黄色福利视频| 中中文字幕亚洲无线码| 啦啦啦在线免费视频| 亚洲av永久中文无码精品综合| 精品免费国产一区二区三区| 亚洲gay片在线gv网站| 亚洲真人日本在线| 最好免费观看高清在线| 亚洲欧洲在线观看| 美丽的姑娘免费观看在线播放| 亚洲免费人成视频观看| 成年在线网站免费观看无广告| 亚洲AV无码一区二区三区电影| 国产免费一区二区三区VR| free哆拍拍免费永久视频| 亚洲AV无码国产精品色午友在线| **真实毛片免费观看| 亚洲熟伦熟女专区hd高清| 可以免费观看的一级毛片| 中文字幕视频在线免费观看| 亚洲精品自产拍在线观看动漫| 青青青免费国产在线视频小草| 国产亚洲精品AAAA片APP| 2048亚洲精品国产| 88av免费观看| 亚洲AV无码一区二区三区性色| 亚洲午夜福利AV一区二区无码| 免费福利视频导航| 羞羞视频在线免费观看| 亚洲av无码乱码国产精品| 成人免费毛片内射美女-百度| 麻豆安全免费网址入口| 337p日本欧洲亚洲大胆色噜噜| 国产福利免费在线观看| 最近的2019免费中文字幕| 中文有码亚洲制服av片| 国产亚洲老熟女视频|