Intent深入解剖

Intent提供了一種通用的消息系統,它允許在你的應用程序和其他的應用程序間傳遞Intent來執行動作和產生事件。使用Intent可以激活

Android應用三種類型的核心組件:活動,服務,和廣播接收者。

Intent可以劃分爲顯式意圖和隱式意圖。

顯式意圖:調用Intent.setComponent()\Intent.setClassName()\Intent.setClass()明確指定了組件名的Intent爲顯式意圖。現時意圖明確指定了要激活的組件是哪個組件。

隱式意圖:沒有明確指定組件名的Intent爲隱式意圖。Android系統會根據隱式意圖中設置的動作(action)、類別(category)、數據(URI和數據類型)找到最合適的組件來處理這個意圖。

建一個新的應用:

在MainActivity中添加方法:

public void openOtherActivity(View v) {

//(沒設數據參數的情況下)只要Intent中的Action和Category都出現在intent-filter中,就能與之匹配,否則匹配失敗。

Intent intent = new Intent();//隱式意圖激活Activity;

  intent.setAction("cn.itcast.zhangxx")

intent.addCategory("cn.itcast.category.java");

intent.setDataAndType(Uri.parse("itcast://www.itcast.cn/liming"),"image/jpeg");

startActivity(intent);

}

新建一個OtherActivity;

在配置文件中配置OtherActivity:

<intent-filter>

<action android:name="cn.itcast.zhangxx"/>

<category android:name="cn.itcast.category.java"/>

<category android:name="android.intent.category.DEFAULT">

<data android:scheme="itcast" android:host="www.itcast.cn" android:path="/liming"/>

<data android:mimeType="image/*"/>//匹配所有圖像類型的文件

</intent-filter>

發佈了57 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章