Android入門:隱式Intent

來源:互聯網
上載者:User


一、隱式意圖介紹


顯式意圖我們前面已經提到,形如:

Intent intent = new Intent();

intent.setClass(this,Other.class);
//此句表示顯式意圖,因為明確設定啟用物件為Other類

startActivity(intent);


顧名思義,隱式意圖就是在不明確設定啟用物件的前提下尋找最匹配的組件,舉個例子,比如有5個人:

(1)A:170cm

(2)B:160cm

(3)C:180cm

(4)D:190cm

(5)E:200cm

如果是顯的話,如果我們要指明選擇A的話會說:”我選擇A.“,但是如果是隱式意圖,則會說:”我要選擇170cm的人“,雖然沒有指明要選A,但會尋找條件最匹配的人。


在intent過濾器中類似於上面例子中的”身高“條件的匹配條件有:

(1)action

(2)category

(3)data:scheme、host、path、type

當在程式中設定了這些啟用組件的條件,程式就會去尋找最匹配的組件,但是注意:只要有一點不匹配,則就是不匹配;

比如:

Intent intent = new Intent();

intent.setAction("a");
//此句只是指定了Action

startActivity(intent);
//尋找最匹配的組件啟用,內部會調用intent.addCategory("android.intent.category.DEFAULT"); 


二、隱式Intent的核心代碼


首先是在AndroidManifest.xml中為某個Activity設定意圖過濾器:


<activity><intent-filter><action android:name="...."/><category android:name="...."/><category android:name="android.intent.category.DEFAULT"/><!--此句一般都要加 --><data android:scheme="..." android:host="..." android:path="/..." android:type="..."/></intent-filter></activity>

以上設定是設定Activity本身的屬性,接下來在程式中要設定的是我們要尋找時匹配的條件:

(1)Intent intent = new Intent();

(2)intent.setAction("....");

(3)intent.addCategory("....");

(4)intent.setData(Uri.parse("...."));
//設定data的scheme、host、path條件

(5)intent.setDataAndType(Uri.parse(""),String type);
//同時設定data的scheme、host、path、type條件

(6)startActivity(intent);
//調用intent.addCategory("android.intent.category.DEFAULT");

三、代碼舉例


情境介紹:在MainActivity中有一個按鈕,點擊按鈕後,會進行隱式Intent匹配,最後尋找到並啟用OtherActivity.

情況1:

<activity       android:name=".OtherActivity"       android:label="OtherActivity" >       <intent-filter>           <action android:name="com.xiazdong.action" />           <category android:name="android.intent.category.DEFAULT" />           <category android:name="com.xiazdong.category" />           <data               android:host="www.xiazdong.com"               android:scheme="xiazdong"/>       </intent-filter></activity>

則代碼為:

Intent intent = new Intent();intent.setAction("com.xiazdong.action");intent.addCategory("com.xiazdong.category");intent.setData(Uri.parse("xiazdong://www.xiazdong.com/xia"));startActivity(intent);//此方法中調用intent.addCategory("android.intent.category.DEFAULT");

情況2:


在<data>中多了一個android:mimeType="text/*",此時不能使用intent.setData,而要使用intent.setDataAndType();

<activity     android:name=".OtherActivity"     android:label="OtherActivity" >         <intent-filter>         <action android:name="com.xiazdong.action" />         <category android:name="android.intent.category.DEFAULT" />         <category android:name="com.xiazdong.category" />         <data            android:host="www.xiazdong.com"            android:scheme="xiazdong" android:mimeType="text/*"/>    </intent-filter></activity>

代碼為:

Intent intent = new Intent();intent.setAction("com.xiazdong.action");intent.addCategory("com.xiazdong.category");intent.setDataAndType(Uri.parse("xiazdong://www.xiazdong.com/xia"), "text/plain");//匹配了text/*startActivity(intent);//此方法中調用intent.addCategory("android.intent.category.DEFAULT");

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.