Intent 解析

一.Intent的介紹

Intent的中文意思是“意圖,意向”,在Android中提供了Intent機制來協助應用間的交互與通訊,Intent負責對應用中一次操作的動作、動作涉及數據、附加數據進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。Intent不僅可用於應用程序之間,也可用於應用程序內部的Activity/Service之間的交互。因此,可以將Intent理解爲不同組件之間通信的“媒介”專門提供組件互相調用的相關信息。

二.Inten啓動組件的方法

Intent可以啓動一個Activity,也可以啓動一個Service,還可以發起一個廣播Broadcasts。具體方法如下:

組件名稱

方法名稱

 

Activity

startActvity( )

startActivity( )

 

Service

startService( )

bindService( )

 

Broadcasts

sendBroadcasts( )

sendOrderedBroadcasts( )

sendStickyBroadcasts( )

三.Intent的屬性

Intent有以下幾個屬性:

動作(Action),數據(Data),分類(Category),類型(Type),組件(Compent)以及擴展信(Extra)。其中最常用的是Action屬性和Data屬性。

1.Intent的Action屬性

Action是指Intent要完成的動作,是一個字符串常量。SDK中定義了一些標準的Action常量如下表所示。

Constant

Target component

Action

ACTION_CALL

activity

Initiate a phone call.

ACTION_EDIT

activity

Display data for the user to edit.

ACTION_MAIN

activity

Start up as the initial activity of a task, with no data input and no returned output.

ACTION_SYNC

activity

Synchronize data on a server with data on the mobile device.

ACTION_BATTERY_LOW

broadcast receiver

A warning that the battery is low.

ACTION_HEADSET_PLUG

broadcast receiver

A headset has been plugged into the device, or unplugged from it.

ACTION_SCREEN_ON

broadcast receiver

The screen has been turned on.

ACTION_TIMEZONE_CHANGED

broadcast receiver

The setting for the time zone has changed.

 下面是一個測試Action常量的例子:

main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <TextView    
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="@string/hello" 
  11.         /> 
  12.     <Button   
  13.         android:text="測試Action屬性" 
  14.         android:id="@+id/getBtn" 
  15.         android:layout_width="wrap_content"   
  16.         android:layout_height="wrap_content"   
  17.         /> 
  18. </LinearLayout> 

 strings.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="hello">測試Action屬性</string> 
  4.     <string name="app_name">IntentActionDemo</string> 
  5. </resources> 

MainActivity.java

  1. package com.android.action.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.  
  10. public class MainActivity extends Activity {  
  11.     private Button getBtn;  
  12.     @Override 
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         getBtn=(Button)findViewById(R.id.getBtn);  
  18.         getBtn.setOnClickListener(new OnClickListener() {  
  19.             @Override 
  20.             public void onClick(View v) {     
  21.                 Intent intent = new Intent();                 
  22.                 intent.setAction(Intent.ACTION_GET_CONTENT);// 設置Intent Action屬性                  
  23.                 intent.setType("vnd.android.cursor.item/phone");// 設置Intent Type 屬性   
  24.                                                                 //主要是獲取通訊錄的內容  
  25.                 startActivity(intent); // 啓動Activity  
  26.             }  
  27.         });          
  28.     }  

效果圖:

2.Intent的Data屬性

Intent的Data屬性是執行動作的URI和MIME類型,不同的Action有不同的Data數據指定。比如:ACTION_EDIT Action應該和要編輯的文檔URI Data匹配,ACTION_VIEW應用應該和要顯示的URI匹配。

3.Intent的Category屬性

Intent中的Category屬性是一個執行動作Action的附加信息。比如:CATEGORY_HOME則表示放回到Home界面,ALTERNATIVE_CATEGORY表示當前的Intent是一系列的可選動作中的一個。下表是SDK文檔中關於Category的信息。

Constant

Meaning

CATEGORY_BROWSABLE

The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.

CATEGORY_GADGET

The activity can be embedded inside of another activity that hosts gadgets.

CATEGORY_HOME

The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed.

CATEGORY_LAUNCHER

The activity can be the initial activity of a task and is listed in the top-level application launcher.

CATEGORY_PREFERENCE

The target activity is a preference panel.

 下面是一個回到Home界面的例子:

main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     >     
  6.     <TextView   
  7.     android:layout_width="fill_parent" 
  8.     android:layout_height="wrap_content"   
  9.     android:text="測試Intent Category"   
  10.     /> 
  11.     <Button   
  12.     android:id="@+id/Button1"   
  13.     android:layout_width="wrap_content" 
  14.     android:layout_height="wrap_content"   
  15.     android:text="轉到Home界面" 
  16.     />    
  17. </LinearLayout> 

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="hello">Hello World, MainActivity!</string> 
  4.     <string name="app_name">IntentCategoryDemo</string> 
  5. </resources> 

MainActivity.java

  1. package com.android.category.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.  
  10. public class MainActivity extends Activity {  
  11.     private Button btn;  
  12.     @Override 
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         btn = (Button)findViewById(R.id.Button1);  
  18.         btn.setOnClickListener(new OnClickListener() {  
  19.             @Override 
  20.             public void onClick(View v) {     
  21.                 Intent intent = new Intent();                 
  22.                 intent.setAction(Intent.ACTION_MAIN);// 添加Action屬性                
  23.                 intent.addCategory(Intent.CATEGORY_HOME);// 添加Category屬性              
  24.                 startActivity(intent);// 啓動Activity  
  25.             }  
  26.         });  
  27.     }  

 效果圖:

 

4.Intent的Type屬性

Intent的Type屬性顯式指定Intent的數據類型(MIME)。一般Intent的數據類型能夠根據數據本身進行判定,但是通過設置這個屬性,可以強制採用顯式指定的類型而不再進行推導。
 

5.Intent的Compent屬性

Intent的Compent屬性指定Intent的的目標組件的類名稱。通常 Android會根據Intent 中包含的其它屬性的信息,比如action、data/type、category進行查找,最終找到一個與之匹配的目標組件。但是,如果 component這個屬性有指定的話,將直接使用它指定的組件,而不再執行上述查找過程。指定了這個屬性以後,Intent的其它所有屬性都是可選的。

 6.Intent的Extra屬性

Intent的Extra屬性是添加一些組件的附加信息。比如,如果我們要通過一個Activity來發送一個Email,就可以通過Extra屬性來添加subject和body。

 下面的例子在第一個Activity的EditText輸入用戶名,該年齡保存在Intent的Extras屬性中。當單擊Button時,會在第二個Activity中顯示用戶名。

first.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical"   
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     >     
  7.     <TextView     
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content"   
  10.         android:text="請輸入用戶名"   
  11.         />        
  12.     <EditText   
  13.         android:id="@+id/EditText1"   
  14.         android:layout_width="fill_parent" 
  15.         android:layout_height="wrap_content" 
  16.         />        
  17.     <Button   
  18.         android:id="@+id/Button1"   
  19.         android:layout_width="wrap_content" 
  20.         android:layout_height="wrap_content"   
  21.         android:text="測試Extras屬性" 
  22.         />        
  23. </LinearLayout> 

second.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical"   
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     >         
  7.     <TextView   
  8.         android:id="@+id/TextView1"   
  9.         android:layout_width="wrap_content"   
  10.         android:layout_height="wrap_content" 
  11.         /> 
  12. </LinearLayout> 

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="hello">Hello World, FirstActivity!</string> 
  4.     <string name="app_name">IntentExtrasDemo</string> 
  5. </resources> 

FirstActivity.java

  1. package com.android.extras.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.  
  11. public class FirstActivity extends Activity {  
  12.     private Button btn;  
  13.     private EditText etx;  
  14.       
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.first);  
  19.           
  20.         btn = (Button)findViewById(R.id.Button1);  
  21.         etx = (EditText)findViewById(R.id.EditText1);  
  22.           
  23.         btn.setOnClickListener(new OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 Intent intent = new Intent();  
  27.                 //設置Intent的class屬性,跳轉到SecondActivity  
  28.                 intent.setClass(FirstActivity.this, SecondActivity.class);  
  29.                 //爲intent添加額外的信息  
  30.                 intent.putExtra("useName", etx.getText().toString());  
  31.                 //啓動Activity  
  32.                 startActivity(intent);  
  33.             }  
  34.         });         
  35.     }  

SecondActivity.java

  1. package com.android.extras.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.  
  8. public class SecondActivity extends Activity {  
  9.     private TextView tv;  
  10.       
  11.     @Override 
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         //設置當前的Activity的界面佈局  
  15.         setContentView(R.layout.second);  
  16.         //獲得Intent  
  17.         Intent intent = this.getIntent();         
  18.         tv = (TextView)findViewById(R.id.TextView1);  
  19.         //從Intent獲得額外信息,設置爲TextView的文本  
  20.         tv.setText(intent.getStringExtra("useName"));  
  21.     }  

注意:在添加第二個Activity SecondActivity的時候,要在AndroidManifest.xml裏面添加上SecondActivity,具體如下,即是在15行</activity>的後面添加上16~18行的代碼。如果不這樣做,就會在模擬器上出現錯誤。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.android.extras.activity" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <uses-sdk android:minSdkVersion="10" /> 
  7.  
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  9.         <activity android:name=".FirstActivity" 
  10.                   android:label="@string/app_name"> 
  11.             <intent-filter> 
  12.                 <action android:name="android.intent.action.MAIN" /> 
  13.                 <category android:name="android.intent.category.LAUNCHER" /> 
  14.             </intent-filter> 
  15.         </activity> 
  16.         <activity android:name=".SecondActivity" 
  17.                   android:label="@string/app_name"> 
  18.         </activity> 
  19.     </application> 
  20. </manifest> 

效果圖:

本文出自 “IT的點點滴滴” 博客,請務必保留此出處http://liangruijun.blog.51cto.com/3061169/634411

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