android intent-filter category.DEFAULT

在顯式Intent消息中,決定目標組件的惟一要素就是組件名稱,所以,若是你的Intent中已經明肯定義了目標組件的名稱,那麼你就徹底不用再 定義其餘Intent內容。而對於隱式Intent則不一樣,因爲沒有明確的目標組件名稱,因此必須由Android系統幫助應用程序尋找與Intent請 求意圖最匹配的組件。具體的選擇方法是:Android將Intent的請求內容和一個叫作Intent Filter的過濾器比較,Intent Filter中包含系統中全部可能的待選組件。若是Intent Filter中某一組件匹配隱式Intent請求的內容,那麼Android就選擇該組件做爲該隱式Intent的目標組件.java

  Android 如何知道應用程序可以處理某種類型的Intent 請求呢? 這須要應用程序在AndroidManifest.xml中聲明本身所含組件的過濾器(便可以匹配哪些Intent請求)。一個沒有聲android

明Intent Filter的組件只能響應指明本身名字的顯式Intent請求,而沒法響應隱式Intent請求。而一個聲明瞭Intent Filter的組件既能夠響應顯式Intent請求,也能夠響應隱式Intent請求。在經過和Intent Filter比較來解析隱式Intent請求時,Android將如下三個因素做爲選擇的參考標準。ide

􀂉 Action測試

􀂉 Dataspa

􀂉 Categoryxml

而Entra和Flag在解析收到Intent時是並不起做用的對象

對於categoryget

對於一個intent要經過種類檢測,intent對象中的每一個種類必須匹配過濾器中的一個。即過濾器可以列出額外的種類,可是intent對象中的種類都必須可以在過濾器中找到,只有一個種類在過濾器列表中沒有,就算種類檢測失敗!it

所以,原則上若是一個intent對象中沒有種類(即種類字段爲空)應該老是經過種類測試,而無論過濾器中有什麼種類。可是有個例外,Android對待全部傳遞給Context.startActivity()的 隱式intent好像它們至少包含"android.intent.category.DEFAULT"(對應CATEGORY_DEFAULT常量)。 所以,活動想要接收隱式intent必需要在intent過濾器中包含"android.intent.category.DEFAULT"。io

注意:"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接收。

因此要想讓其接收,必需要加上這句話!

相關文章
相關標籤/搜索