Intent入門指南

來源:互聯網
上載者:User

一、什麼是Intent:

在一個Android應用中,Intent是對執行某個操作的一個抽象描述,Intent 負責提供組件之間相互調用的相關資訊傳遞,實現調用者和被調用者之間的解耦。

二、Intent的屬性:

Intent是對執行某個操作的一個抽象描述,其描述的內容包括,對執行動作Action的描述、對操作資料的描述、還有4種附加屬性的描述。分別介紹如下:

Action

,對執行動作的描述,是個字串,是對所將執行的動作的描述,在Intent類中定義了一些字串常量作為標準動作,譬如:

view source
print
?
1 public static final String ACTION_DIAL = "android.intent.action.DIAL"
2 public static final String ACTION_SENDTO = "android.intent.action.SENDTO";

我們還可以自訂Action,並定義相應的Activity來處理我們自訂的行為。

data
,是對執行動作所要操作的資料的描述,Android中採用URI來表示資料,譬如在連絡人應用中,
指向連絡人1的URI可能為:content://contacts/1 (由content provider提供的資料類型是content) 。
結合Action和data可以基本表達出意圖,

  • VIEW_ACTION content://contacts/1
    — 顯示標識符為”1″的連絡人的詳細資料
  • EDIT_ACTION content://contacts/1
    — 編輯標識符為”1″的連絡人的詳細資料
  • VIEW_ACTION content://contacts/
    — 顯示所有連絡人的列表
  • PICK_ACTION content://contacts/
    — 顯示所有連絡人的列表,並且允許使用者在列表中選擇一個連絡人,然後把這個連絡人返回給父activity。例如:電子郵件用戶端可以使用這個 Intent,要求使用者在連絡人清單中選擇一個連絡人。

除了Action和data之外,還有4個屬性。

catagory 類別
,是被請求組件的額外描述資訊,Intent類中也定義了一組字串常量表示Intent不同的類別。完整的列表可以看API文檔中Intent類的相應部分。

view source
print
?
1 public
static
final
String CATEGORY_LAUNCHER =
"android.intent.category.LAUNCHER"
;
2 public
static
final
String CATEGORY_PREFERENCE =
"android.intent.category.PREFERENCE"
;

extra 附加資訊,
除了data之外,還可以通過extra附加資訊,extra屬性使用Bundle類型
進行資料傳遞,我們可以把Bundle當做HashMap來理解,附加資料可以通過 intent.putExtras() 和
intent.getExtras() 進行傳入和讀取。 就像這樣,擷取附加資訊: Bundle bundle =
intent.getExtras();

添加附加資訊: Bundle bundle = new Bundle(); intent.putExtras(bundle);

component 組件
,顯式指定Intent的目標組件的名稱。如果指定了component屬性,系統會直接使用它指定的組件,而非匹配尋找。

type 資料類型,
顯式指定Intent的資料類型,一般Intent的資料類型都能夠根據資料本身進行判定,但是通過設定這個屬性,可以強制採用顯式指定的類型和不再進行推導。

三、解析Intent

Intent是一種在不同組件之間傳遞的請求資訊,是應用程式發出的請求和意圖,作為一個完整的訊息傳遞機制,Intent不僅需要發送端,還需要接收端。

當指定了component屬性後,就是顯式的指定了目標組件,也就是接收端。如果沒有明確指定目標組件,那麼Android系統會使用 Intent 裡的(action,data,category)三個屬性來尋找和匹配接收端。

四、IntentFilter

應用程式組件可以使用IntentFilter來向系統說明自己可以響應和處理那些Intent請求。組件一般通過AndroidManifest.xml檔案的<Intent-Filter>描述。

<activity android:name=”.MainTinyPhone” android:label=”@string/app_name”>

<intent-filter>

<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

五、簡單的撥打到電話的例子

這是一個簡單的撥打到電話的程式,它可以截獲手機上的撥號按鍵發出的請求,自己輸入號碼,並把資料傳給系統的打電話程式,從而實現打電話功能。

先上:

按綠色的撥號按鈕的時候會呼出一個菜單讓你選擇使用哪個撥號程式,

我們選擇簡單撥號程式,出現輸入框可以填寫電話號碼,並撥號

點擊撥號後,調用了系統的撥號程式。

最終實現了撥號:

再上代碼,MainTinyDial.java的代碼:

view source
print
?
01 package
andorid.basic.lesson8;
02  
03 import
android.app.Activity;
04 import
android.content.Intent;
05 import
android.net.Uri;
06 import
android.os.Bundle;
07 import
android.view.View;
08 import
android.widget.Button;
09 import
android.widget.EditText;
10  
11 public
class
MainTinyDial
extends
Activity {
12     
/** Called when the activity is first created. */
13     
@Override
14     
public
void
onCreate(Bundle savedInstanceState) {
15         
super
.onCreate(savedInstanceState);
16  
17         
// 按照main.xml來渲染用戶介面
18         
setContentView(R.layout.main);
19  
20         
// 找到存放電話號碼的可編輯文字框
21         
final
EditText PhoneNumberEditText = (EditText) findViewById(R.id.PhoneNumberEditText);
22  
23         
// 找到撥號按鈕
24         
Button button = (Button) findViewById(R.id.Button01);
25  
26         
// 為撥號按鈕設定一個點擊事件觀察者
27         
button.setOnClickListener(
new
Button.OnClickListener() {
28             
//實現監聽器介面的匿名內部類,其中監聽器本身是View類的內部介面
29  
30             
//實現介面必須實現的onClick方法
31             
@Override
32             
public
void
onClick(View v) {
33                 
// 獲得可編輯文字框中的值,也就是電話號碼
34                 
String phoneNumber = PhoneNumberEditText.getText().toString();
35                 
// new Intent(行為,資料),其中action_dial是撥號行為,資料是電話號碼
36                 
Intent intent =
new
Intent(Intent.ACTION_DIAL, Uri
37                         
.parse(
"tel://
"

+ phoneNumber));
38                 
// 去調用那些可以處理撥號行為的Activity
39                 
startActivity(intent);
40             
}
41         
});
42  
43     
}
44 }

介面布局的main.xml代碼:

view source
print
?
1 <?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
2  
3     
<
button
>
4     
</
button
>

程式總體設定的AndroidManifest.xml的代碼:

view source
print
?
1 <?xml version=
"1.0"
encoding=
"utf-8"
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.