????:Android基本的設(shè)計(jì)理念是鼓勵(lì)減少組件間的耦合,因此Android提供了Intent (意圖) ,Intent提供了一種通用的消息系統(tǒng),它允許在你的應(yīng)用程序與其它的應(yīng)用程序間傳遞Intent來(lái)執(zhí)行動(dòng)作和產(chǎn)生事件。使用Intent可以激活A(yù)ndroid應(yīng)用的三個(gè)核心組件:活動(dòng)、服務(wù)和廣播接收器(四大組件中還有一個(gè)是“內(nèi)容提供者”--Content Provider )所謂的“意圖”,不要想的太復(fù)
Android基本的設(shè)計(jì)理念是鼓勵(lì)減少組件間的耦合,因此Android提供了Intent (意圖) ,Intent提供了一種通用的消息系統(tǒng),它允許在你的應(yīng)用程序與其它的應(yīng)用程序間傳遞Intent來(lái)執(zhí)行動(dòng)作和產(chǎn)生事件。使用Intent可以激活A(yù)ndroid應(yīng)用的三個(gè)核心組件:活動(dòng)、服務(wù)和廣播接收器(四大組件中還有一個(gè)是“內(nèi)容提供者”--Content Provider )
所謂的“意圖”,不要想的太復(fù)雜,通俗點(diǎn)說(shuō)就是“告訴Android系統(tǒng),你想做什么事兒...”,這是我個(gè)人的一點(diǎn)淺見(jiàn)。
Intent可以劃分成顯式意圖和隱式意圖。
顯式意圖:調(diào)用Intent.setComponent()或Intent.setClass()方法明確指定了組件名的Intent為顯式意圖,顯式意圖明確指定了Intent應(yīng)該傳遞給哪個(gè)組件。
隱式意圖:沒(méi)有明確指定組件名的Intent為隱式意圖。 Android系統(tǒng)會(huì)根據(jù)隱式意圖中設(shè)置的動(dòng)作(action)、類別(category)、數(shù)據(jù)(URI和數(shù)據(jù)類型)找到最合適的組件來(lái)處理這個(gè)意圖。
在使用Android開(kāi)發(fā)“電話撥打功能”的時(shí)候,我們通常會(huì)使用類似如下的代碼:
package dinglang.com.phone; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class phone extends Activity { /** Called when the activity is first created. */ private EditText phoneText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //找到撥號(hào)按鈕 Button button =(Button)this.findViewById(R.id.btnCall); phoneText =(EditText)this.findViewById(R.id.txtPhoneNumber); //給button注冊(cè)事件 button.setOnClickListener(new ButtonLis()); } //定義一個(gè)私有的內(nèi)部類,而且還是不能被繼承的。繼承至OnClickListener接口 private final class ButtonLis implements View.OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub //獲得文本框輸入的電話號(hào)碼 String mobile = phoneText.getText().toString(); //調(diào)用系統(tǒng)底層API,啟動(dòng)撥號(hào)程序去撥打這個(gè)號(hào)碼 Intent intent =new Intent("android.intent.action.CALL",Uri.parse("tel:"+mobile)); startActivity(intent);//內(nèi)部會(huì)自動(dòng)添加anroid.intent.category.DEFAULT } } }
其中“android.intent.action.CALL”是意圖過(guò)濾器中的“動(dòng)作名稱”。
有人可能會(huì)疑問(wèn),我在“電話撥號(hào)程序”中好像沒(méi)有修改項(xiàng)目清單,沒(méi)有往里面添加任何內(nèi)容,好像也可以??!因?yàn)殡娫挀芴?hào)功能屬于Android系統(tǒng)內(nèi)置,系統(tǒng)會(huì)自動(dòng)去匹配(比較特殊,所以不需要用戶手動(dòng)去添加)。自動(dòng)生成類似代碼:
<intent-filter> <action android:name="android.intent.action.CALL" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="tel" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.CALL" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/phone" /> </intent-filter>
下面是一個(gè)小例子,Activity端的代碼:
package cn.itcast.intent; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("cn.itcast.lao.li"); //intent.setData(Uri.parse("itcast://www.itcast.cn/person")) //intent.setType("image/gif");//使用setType方法后,將會(huì)清空前面的設(shè)置,所以可以使用下面這種方式 intent.setDataAndType(Uri.parse("itcast://www.itcast.cn/person"), "image/gif"); startActivity(intent); } }); } }
通常情況,我們?cè)谕ㄟ^(guò)隱式意圖Intent激活其他組件時(shí),為了匹配,需要在項(xiàng)目清單文件中類似代碼:
<activity android:name=".OtherActivity" android:label="@string/app_name"> <intent-filter> <action android:name="cn.itcast.lao.li" /> <action android:name="cn.itcast.lao.zhang" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="cn.itcast.category.li" /> <data android:scheme="itcast" android:host="www.itcast.cn" android:path="/person"/> <data android:mimeType="image/gif"/> </intent-filter> </activity>
現(xiàn)在應(yīng)該明白,意圖過(guò)濾器的作用了吧?