目標組件一般要通過Intent來聲明自己的條件,一般通過組件中的<intent-filter>元素來過濾。
Intent在由以下幾個部分組成:動作(action),數據(data),分類(Category),類型(Type),組件(Component),和擴展信息(Extra)。
Intent在尋找目標組件的時候有兩種方法:第一,通過組件名稱直接指定;第二,通過Intent Filter過濾指定
Intent啓動不同組件的方法
組件名稱 |
方法名稱 |
Activity |
startActivity() startActivityForResult() |
Service |
startService() bindService() |
Broadcasts |
sendBroadcast() sendOrderedBroadcast() sendStickyBroadcast() |
常見的Activity Action Intent常量
常量名稱 |
常量值 |
意義 |
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.GET_CONTENT |
顯示打電話面板 |
ACITON_CALL |
android.intent.action.DIAL |
直接打電話 |
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_FACTORY_TEST |
android.intent.action.FACTORY_TEST |
工廠測試入口點 |
常見的BroadcastIntent Action常量
BroadcastIntent Action字符串常量 |
描述 |
ACTION_TIME_TICK |
系統時間每過一分鐘發出的廣播 |
ACTION_TIME_CHANGED |
系統時間通過設置發生了變化 |
ACTION_TIMEZONE_CHANGED |
時區改變 |
ACTION_BOOT_COMPLETED |
系統啓動完畢 |
ACTION_PACKAGE_ADDED |
新的應用程序apk包安裝完畢 |
ACTION_PACKAGE_CHANGED |
現有應用程序apk包改變 |
ACTION_PACKAGE_REMOVED |
現有應用程序apk包被刪除 |
ACTION_UID_REMOVED |
用戶id被刪除 |
Intent的Action和Data屬性匹配
Action屬性 |
Data屬性 |
說明 |
ACTION_VIEW |
content://contacts/people/1 |
顯示id爲1的聯繫人信息 |
ACTION_DIAL |
content://contacts/people/1 |
將id爲1的聯繫人電話號碼顯示在撥號界面中 |
ACITON_VIEW |
顯示電話爲123的聯繫人信息 |
|
ACTION_VIEW |
在瀏覽器中瀏覽該網站 |
|
ACTION_VIEW |
播放MP3 |
|
ACTION_VIEW |
geo:39.2456,116.3523 |
顯示地圖 |
常見的Category常量
Category字符串常量 |
描述 |
CATEGORY_BROWSABLE |
目標Activity能通過在網頁瀏覽器中點擊鏈接而激活(比如,點擊瀏覽器中的圖片鏈接) |
CATEGORY_GADGET |
表示目標Activity可以被內嵌到其他Activity當中 |
CATEGORY_HOME |
目標Activity是HOME Activity,即手機開機啓動後顯示的Activity,或按下HOME鍵後顯示的Activity |
CATEGORY_LAUNCHER |
表示目標Activity是應用程序中最優先被執行的Activity |
CATEGORY_PREFERENCE |
表示目標Activity是一個偏愛設置的Activity |
常見的Extra常量
Extra鍵值字符串常量 |
描述 |
EXTRA_BCC |
裝有郵件密送地址的字符串數組 |
EXTRA_CC |
裝有郵件抄送地址的字符串數組 |
EXTRA_EMAIL |
裝有郵件發送地址的字符串數組 |
EXTRA_INTENT |
使用ACTION_PICK_ACTIVITY動作時裝有Intent選項的鍵 |
EXTRA_KEY_EVENT |
觸發該Intent的案件的KeyEvent對象 |
EXTRA_PHONE_NUMBER |
使用撥打電話相關的Action時,電話號碼字符串的鍵,類型爲String |
EXTRA_SHORTCUT_ICON |
使用ACTION_CREATE_SHORTCUT在HomeActivity創建快捷方式時,對快捷方式的描述信息。其中ICON和ICON_RESOURCE描述的是快捷方式的圖標,類型分別爲Bitmap和ShortcutIconResource。INTENT描述的是快捷方式相對應的Intent對象。NAME描述的是快捷方式的名字。 |
EXTRA_SHORTCUT_ICON_RESOURCE |
|
EXTRA_SHORTCUT_INTENT |
|
EXTRA_SHORTCUT_NAME |
|
EXTRA_SUBJECT |
描述信息主題的鍵 |
EXTRA_TEXT |
使用ACTION_SEND動作時,用來描述要發送的文本信息,類型爲CharSequence |
EXTRA_TITLE |
使用ACTION_CHOOSER動作時,描述對話框標題的鍵,類型爲CharSequence |
EXTRA_UID |
使用ACTION_UID_REMOVED動作時,描述刪除的用戶id的鍵,類型爲int |
Android.telephony包中的類
類名 |
描述 |
CellLocation |
表示設備位置的抽象類 |
PhoneNumberFormattingTextWather |
監視一個TextView控件,如果有電話號碼輸入,則用formatNumber()方法處理電話號碼 |
PhoneNumberUtils |
包含各種處理電話號碼字符串的使用工具 |
PhoneStateListener |
監視手機中電話狀態變化的監聽類 |
ServiceState |
包含電話狀態和相關的服務信息 |
TelephonyManager |
提供對手機中電話服務信息的訪問 |
與短信服務相關的類主要在包android.telephony.gsm中
類名 |
描述 |
GsmCellLocation |
表示GSM手機的基站位置 |
SmsManager |
管理各種短信操作 |
SmsMessage |
表示具體的短信 |
例子一:計算器
- package org.loulijun.calculate;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Calculater extends Activity {
- /** Called when the activity is first created. */
- private EditText numberone;
- private EditText numbertwo;
- private Button mybutton;
- private TextView symbol;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- numberone=(EditText)findViewById(R.id.numberone);
- numbertwo=(EditText)findViewById(R.id.numbertwo);
- mybutton=(Button)findViewById(R.id.mybutton);
- symbol=(TextView)findViewById(R.id.symbol);
- mybutton.setOnClickListener(new Calculate());
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // TODO Auto-generated method stub
- menu.add(0, 1, 1, R.string.exit);
- menu.add(0,2,2,R.string.about);
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // TODO Auto-generated method stub
- if(item.getItemId()==1)
- {
- finish();
- }
- else if(item.getItemId()==2)
- {
- Toast.makeText(this, "親愛的你可知,我有多麼思念你!", Toast.LENGTH_SHORT).show();
- }
- return super.onOptionsItemSelected(item);
- }
- class Calculate implements OnClickListener
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- String number1=numberone.getText().toString();
- String number2=numbertwo.getText().toString();
- Intent intent=new Intent();
- intent.putExtra("num1",number1);
- intent.putExtra("num2", number2);
- intent.setClass(Calculater.this,Result.class);
- Calculater.this.startActivity(intent);
- }
- }
- }
package org.loulijun.calculate;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
- public class Result extends Activity {
- private TextView textview;
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.result);
- textview=(TextView)findViewById(R.id.textview);
- Intent intent=getIntent();
- String numberone=intent.getStringExtra("num1");
- String numbertwo=intent.getStringExtra("num2");
- Double d1=Double.parseDouble(numberone);
- Double d2=Double.parseDouble(numbertwo);
- Double result=d1/d2;
- textview.setText(numberone+" / "+numbertwo+" = "+result);
- }
- }
main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/numberone"
- android:hint="請輸入數字"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/symbol"
- android:text="@string/symbol"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/numbertwo"
- android:hint="請輸入數字"
- />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/mybutton"
- android:text="@string/calculate"
- />
- </LinearLayout>
result.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textview"
- />
- </LinearLayout>
注意:result.java需要註冊到AndroidManifest.xml文件中
<activity android:name=".Result" android:label="@string/result">
</activity>
運行結果如下:
例子二:調用系統內置action
- package org.hualang.intent;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class IntentTest extends ListActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 菜單項數組
- String[] menus = { "查看電話信息", "編輯電話信息", "顯示撥打電話界面","直接打電話","訪問瀏覽器","訪問地圖"};
- // 將菜單項數組設置爲ListView的列表項展示
- setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, menus));
- getListView().setTextFilterEnabled(true);
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- Intent intent = new Intent();
- Uri uri ;
- String data;
- switch (position) {
- // 查看_id 爲1的用戶電話信息
- case 0:
- data = "content://contacts/people/1";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 編輯_id 爲1的用戶電話信息
- case 1:
- data = "content://contacts/people/1";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_EDIT);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 顯示撥打電話界面
- case 2:
- data = "tel:13800138000";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_DIAL);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 直接打電話
- case 3:
- data = "tel:13800138000";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_CALL);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 訪問瀏覽器
- case 4:
- data = "http://www.google.com";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 訪問地圖
- case 5:
- data = "geo:39.92,116.46";
- uri = Uri.parse(data);
- intent = new Intent(Intent.ACTION_VIEW,uri);
- startActivity(intent);
- break;
- default:
- break;
- }
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.hualang.intent"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".IntentTest"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
- </manifest>
運行結果如下:
當點擊直接打電話後