摘自http://www.cnblogs.com/allin/articles/1728488.html和http://java-admin.iteye.com/blog/725282

在显式Intent消息中,决定目标组件的唯一要素就是组件名称,因此,如果你的Intent中已经明确定义了目标组件的名称,那么你就完全不用再 定义其他Intent内容。而对于隐式Intent则不同,由于没有明确的目标组件名称,所以必须由Android系统帮助应用程序寻找与Intent请 求意图最匹配的组件。具体的选择方法是:Android将Intent的请求内容和一个叫做Intent Filter的过滤器比较,Intent Filter中包含系统中所有可能的待选组件。如果Intent Filter中某一组件匹配隐式Intent请求的内容,那么Android就选择该组件作为该隐式Intent的目标组件.

  Android 如何知道应用程序能够处理某种类型的Intent 请求呢? 这需要应用程序在AndroidManifest.xml中声明自己所含组件的过滤器(即可以匹配哪些Intent请求)。一个没有声

明Intent Filter的组件只能响应指明自己名字的显式Intent请求,而无法响应隐式Intent请求。而一个声明了Intent Filter的组件既可以响应显式Intent请求,也可以响应隐式Intent请求。在通过和Intent Filter比较来解析隐式Intent请求时,Android将以下三个因素作为选择的参考标准。

􀂉 Action

􀂉 Data

􀂉 Category

而Entra和Flag在解析收到Intent时是并不起作用的

对于category

对于一个intent要通过种类检测,intent对象中的每个种类必须匹配过滤器中的一个。即过滤器能够列出额外的种类,但是intent对象中的种类都必须能够在过滤器中找到,只有一个种类在过滤器列表中没有,就算种类检测失败!

因此,原则上如果一个intent对象中没有种类(即种类字段为空)应该总是通过种类测试,而不管过滤器中有什么种类。但是有个例外,Android对待所有传递给Context.startActivity()的 隐式intent好像它们至少包含"android.intent.category.DEFAULT"(对应CATEGORY_DEFAULT常量)。 因此,活动想要接收隐式intent必须要在intent过滤器中包含"android.intent.category.DEFAULT"。

注意:"android.intent.action.MAIN" 和 "android.intent.category.LAUNCHER"设置,它们分别标记活动开始新的任务和带到启动列表界面。它们可以包 含"android.intent.category.DEFAULT"到种类列表,也可以不包含。

所以蓝牙BluetoothOppReceiver.java中隐式发送如下:

   Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);                         
    in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);                           
   in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,                                 
                        BluetoothDevicePicker.FILTER_TYPE_TRANSFER);                                  
                       in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,                              
                        Constants.THIS_PACKAGE_NAME);                                                 
                     in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,                                
                       BluetoothOppReceiver.class.getName());                                        
                        in1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                                          
        context.startActivity(in1);

而BluetoothSettings在接收的时候,

在AndroidManifest.xml中

对于BluetoothSettings这个activity

<intent-filter>
        <action android:name="android.bluetooth.devicepicker.action.LAUNCH" />
              <!--   <category android:name="android.intent.category.DEFAULT" />-->
       </intent-filter>

如果把这段注释掉的话,那么发送的ACTION_LAUNCH意图将不会被BluetoothSettings接收。

所以要想让其接收,必须要加上这句话!

posted on 2011-05-25 15:37  snowdrop  阅读(4670)  评论(0编辑  收藏  举报