Intent表明了啓動某個程序組件的「意圖」,實際上Intent對象不只能夠啓動本應用內程序組件,也可啓動Android系統的其餘應用的程序組件,包括系統自帶的程序組件——只要權限容許。android
實際上Android內部提供了大量標準Action、Category常量,其中用於啓動Activity的標準Action常量及對應的字符串如表5.2所示。瀏覽器
Action常量 | 對應字符串 | 簡單說明 |
---|---|---|
ACTION_MAIN | android.intent.action.MAIN | 應用程序入口 |
ACTION_VIEW |
android.intent.action.VIEW |
顯示指定數據 |
ACTION_ATTACH_DATA |
android.intent.action.ATTACH_DATA |
指定某塊數據將被附加到其餘地方 |
ACTION_EDIT |
android.intent.action.EDIT |
編輯指定數據 |
ACTION_PICK |
android.intent.action.PICK |
從列表中選擇某項並返回所選的數據 |
ACTION_CHOOSER |
android.intent.action.CHOOSER |
顯示一個Activity選擇器 |
ACTION_GET_CONTENT |
android.intent.action.GET_CONTENT |
讓用戶選擇數據,並返回所選數據 |
ACTION_DIAL |
android.intent.action.DIAL |
顯示撥號面板 |
ACTION_CALL | android.intent.action.CALL |
直接向指定用戶打電話 |
ACTION_SEND |
android.intent.action.SEND |
向其餘人發送數據 |
ACTION_SENDTO |
android.intent.action.SENDTO |
向其餘人發送消息 |
ACTION_ANSWER |
android.intent.action.ANSWER |
應答電話 |
ACTION_INSERT |
android.intent.action.INSERT |
插入數據 |
ACTION_DELETE |
android.intent.action.DELETE |
刪除數據 |
ACTION_RUN |
android.intent.action.RUN |
運行數據 |
ACTION_SYNC |
android.intent.action.SYNC |
執行數據同步 |
ACTION_PICK_ACTIVITY |
android.intent.action.PICK_ACTIVITY |
用於選擇Activity |
ACTION_SEARCH |
android.intent.action.SEARCH |
執行搜索 |
ACTION_WEB_SEARCH |
android.intent.action.WEB_SEARCH |
執行Web搜索 |
ACTION_BATTERY_LOW |
android.intent.action.ACTION_BATTERY_LOW | 電量低 |
ACTION_MEDIA_BUTTON | android.intent.action.ACTION_MEDIA_BUTTON | 按下媒體按鈕 |
ACTION_PACKAGE_ADDED | android.intent.action.ACTION_PACKAGE_ADDED | 添加包 |
ACTION_PACKAGE_REMOVED | android.intent.action.ACTION_PACKAGE_REMOVED | 刪除包 |
ACTION_FACTORY_TEST | android.intent.action.FACTORY_TEST | 工廠測試的入口點 |
ACTION_BOOT_COMPLETED | android.intent.action.BOOT_COMPLETED | 系統啓動完成 |
ACTION_TIME_CHANGED | android.intent.action.ACTION_TIME_CHANGED | 時間改變 |
ACITON_DATE_CHANGED | android.intent.action.ACTION_DATE_CHANGED | 日期改變 |
ACTION_TIMEZONE_CHANGED | android.intent.action.ACTION_TIMEZONE_CHANGED | 時區改變 |
ACTION_MEDIA_EJECT | android.intent.action.MEDIA_EJECT | 插入或拔出外部媒體 |
標準Category常量及對應的字符串如表5.3所示安全
Category常量 | 對應字符串 | 簡單說明 |
---|---|---|
CATEGORY_DEFAULT | android.intent.category.DEFAULT | 默認的Category |
CATEGORY_BROWSABLE |
android.intent.category.BROWSABLE |
指定該Activity能被瀏覽器安全調用 |
CATEGORY_TAB |
android.intent.category.TAB |
指定該Activity做爲TabActivity的Tab頁 |
CATEGORY_LAUNCHER |
android.intent.category.LAUNCHER |
Activity顯示頂級程序列表中 |
CATEGORY_INFO |
android.intent.category.INFO |
用於提供包信息 |
CATEGORY_HOME |
android.intent.category.HOME |
設置該Activity隨系統啓動而運行 |
CATEGORY_PREFERENCE |
android.intent.category.PREFERENCE |
該Activity是參數面板 |
CATEGORY_TEST |
android.intent.category.TEST |
該Activity是一個測試 |
CATEGORY_CAR_DOCK |
android.intent.category.CAR_DOCK |
指定手機被插入汽車底座(硬件)時運行該Activity |
CATEGORY_DESK_DOCK |
android.intent.category.DESK_DOCK |
指定手機被插入桌面底座(硬件)時運行該Activity |
CATEGORY_CAR_MODE |
android.intent.category.CAR_MODE |
設置該Activity可在車載環境下使用 |
表5.二、表5.3所列出的都只是部分較爲經常使用的Action常量、Category常量、關於Intent所提供的所有Action常量、Category常量,應參考Android API文檔中關於Intent的說明。app
下面將以兩個實例來介紹Intent系統Action、系統Category的用法。ide
實例:查看並獲取聯繫人電話佈局
這個程序將會在程序中提供一個按鈕,用戶單擊該按鈕時會顯示系統的聯繫人列表,當用戶單擊指定聯繫人以後,程序將會顯示該聯繫人的名字、電話。測試
該程序的界面佈局代碼以下:this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal" > <!-- 顯示 聯繫人姓名的文本框 --> <EditText android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false"/> <!-- 顯示聯繫人的電話的文本框 --> <EditText android:id="@+id/phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false"/> <Button android:id="@+id/bn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看聯繫人"/> </LinearLayout>
上面的界面佈局中包含了兩個文本框,一個按鈕,其中按鈕用於瀏覽系統聯繫人列表並選擇其中的聯繫人。兩個文本框分別用於顯示聯繫人的名字、電話號碼。spa
該程序的Java代碼以下。.net
package com.example.studyintent; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.app.Activity; import android.content.CursorLoader; import android.content.Intent; import android.database.Cursor; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class SysAction extends Activity { final int PICK_CONTACT=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sys_action); Button bn=(Button)findViewById(R.id.bn); //爲bn按鈕綁定事件監聽器 bn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub //建立Intent Intent intent=new Intent(); //設置Intent的Action屬性 intent.setAction(Intent.ACTION_GET_CONTENT); //設置Intent的Type屬性 intent.setType("vnd.android.cursor.item/phone"); //啓動Activity,並但願獲取該Activity的結果 startActivityForResult(intent,PICK_CONTACT); }}); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case (PICK_CONTACT): if(resultCode==Activity.RESULT_OK) { //獲取返回的數據 Uri contactData=data.getData(); CursorLoader cursorLoader=new CursorLoader(this,contactData,null,null,null,null); //查詢聯繫人信息 Cursor cursor=cursorLoader.loadInBackground(); //若是查詢到指定的聯繫人 if(cursor.moveToFirst()) { String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); //獲取聯繫人的名字 String name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); String phoneNumber="此聯繫人暫未輸入電話號碼"; //根據聯繫人查詢該聯繫人的詳細信息 Cursor phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone._ID+"="+contactId, null, null); if(phones.moveToFirst()) { //取出電話號碼 phoneNumber=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } //關閉遊標 phones.close(); EditText show=(EditText)findViewById(R.id.show); //顯示聯繫人的名稱 show.setText(name); EditText phone=(EditText)findViewById(R.id.phone); //顯示聯繫人的電話號碼 phone.setText(phoneNumber); } //關閉遊標 cursor.close(); } break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.sys_action, menu); return true; } }
運行上面的程序,單擊程序界面中「查看聯繫人」按鈕,程序將會顯示如圖5.4所示的界面。
在圖5.4所示的聯繫人列表中單擊某個聯繫人,系統將會自動返回上一個Activity,程序會在上一個Activity中顯示所選聯繫人的名字和電話,如圖5.5所示。
上面的Intent對象除了設置Action屬性以外,還設置了Type屬性。
須要指出的是,由上面的程序須要查看系統聯繫人信息,所以不要忘了向該應用的Android Manifest.xml文件中增長相應的權限,也就是在AndroidManifest.xml文件中增長以下配置。
<uses-permission android:name="android.permission.READ_CONTACTS" />
實例:返回系統Home桌面
接下來的示例將會提供,當用戶單擊該按鈕時,系統將會返回Home界面。這也須要Intent來實現,程序爲Intent設置合適的Action、合適的Category屬性,並根據該Intent來啓動Activity便可返回Home界面。該示例程序以下。
package com.example.studyintent; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ReturnHome extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_return_home); Button bn=(Button)findViewById(R.id.bn); bn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub //建立Intent對象 Intent intent=new Intent(); //爲Intent設置Action、Category屬性 intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.return_home, menu); return true; } }
上面的程序中粗體字代碼設置了Intent的Action爲android.intent.action.MAIN字符串、Category屬性爲android.intent.category.HOME字符串,知足該Intent的Activity其實就是Android系統的Home界面。所以運行上面程序時「單擊」按鈕便可返回Home界面。