玩轉Android---組件篇

ntent的中文意思是“意圖,目的”的意思,可以理解爲不同組件之間通信的“媒介”或者“信使”。

 

目標組件一般要通過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被刪除

 

IntentActionData屬性匹配

Action屬性

Data屬性

說明

ACTION_VIEW

content://contacts/people/1

顯示id1的聯繫人信息

ACTION_DIAL

content://contacts/people/1

id1的聯繫人電話號碼顯示在撥號界面中

ACITON_VIEW

tel:123

顯示電話爲123的聯繫人信息

ACTION_VIEW

http://www.google.com

在瀏覽器中瀏覽該網站

ACTION_VIEW

file://sdcard/mymusic.mp3

播放MP3

ACTION_VIEW

geo:39.2456,116.3523

顯示地圖

 

 

常見的Category常量

Category字符串常量

描述

CATEGORY_BROWSABLE

目標Activity能通過在網頁瀏覽器中點擊鏈接而激活(比如,點擊瀏覽器中的圖片鏈接)

CATEGORY_GADGET

表示目標Activity可以被內嵌到其他Activity當中

CATEGORY_HOME

目標ActivityHOME 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_SHORTCUTHomeActivity創建快捷方式時,對快捷方式的描述信息。其中ICONICON_RESOURCE描述的是快捷方式的圖標,類型分別爲BitmapShortcutIconResourceINTENT描述的是快捷方式相對應的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

表示具體的短信

 

例子一:計算器

 

Java代碼  收藏代碼
  1. package org.loulijun.calculate;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.Menu;  
  7. import android.view.MenuItem;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. public class Calculater extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.     private EditText numberone;  
  18.     private EditText numbertwo;  
  19.     private Button mybutton;  
  20.     private TextView symbol;  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         numberone=(EditText)findViewById(R.id.numberone);  
  26.         numbertwo=(EditText)findViewById(R.id.numbertwo);  
  27.         mybutton=(Button)findViewById(R.id.mybutton);  
  28.         symbol=(TextView)findViewById(R.id.symbol);  
  29.         mybutton.setOnClickListener(new Calculate());  
  30.           
  31.     }  
  32.       
  33.     @Override  
  34.     public boolean onCreateOptionsMenu(Menu menu) {  
  35.         // TODO Auto-generated method stub  
  36.         menu.add(011, R.string.exit);  
  37.         menu.add(0,2,2,R.string.about);  
  38.         return super.onCreateOptionsMenu(menu);  
  39.     }  
  40.   
  41.     @Override  
  42.     public boolean onOptionsItemSelected(MenuItem item) {  
  43.         // TODO Auto-generated method stub  
  44.         if(item.getItemId()==1)  
  45.         {  
  46.             finish();  
  47.         }  
  48.         else if(item.getItemId()==2)  
  49.         {  
  50.             Toast.makeText(this"親愛的你可知,我有多麼思念你!", Toast.LENGTH_SHORT).show();  
  51.         }  
  52.         return super.onOptionsItemSelected(item);  
  53.     }  
  54.   
  55.     class Calculate implements OnClickListener  
  56.     {  
  57.         @Override  
  58.         public void onClick(View v) {  
  59.             // TODO Auto-generated method stub  
  60.             String number1=numberone.getText().toString();  
  61.             String number2=numbertwo.getText().toString();  
  62.             Intent intent=new Intent();  
  63.             intent.putExtra("num1",number1);  
  64.             intent.putExtra("num2", number2);  
  65.             intent.setClass(Calculater.this,Result.class);  
  66.             Calculater.this.startActivity(intent);  
  67.         }  
  68.     }  
  69. }  

 package org.loulijun.calculate;

Java代碼  收藏代碼
  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.widget.TextView;  
  5.   
  6. public class Result extends Activity {  
  7.     private TextView textview;  
  8.     public void onCreate(Bundle savedInstanceState)  
  9.     {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.result);  
  12.         textview=(TextView)findViewById(R.id.textview);  
  13.         Intent intent=getIntent();  
  14.         String numberone=intent.getStringExtra("num1");  
  15.         String numbertwo=intent.getStringExtra("num2");  
  16.         Double d1=Double.parseDouble(numberone);  
  17.         Double d2=Double.parseDouble(numbertwo);  
  18.         Double result=d1/d2;  
  19.           
  20.         textview.setText(numberone+" / "+numbertwo+" = "+result);  
  21.     }  
  22. }  

 main.xml

Java代碼  收藏代碼
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6.     <EditText  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:id="@+id/numberone"  
  10.         android:hint="請輸入數字"  
  11.     />  
  12.     <TextView  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/symbol"  
  16.         android:text="@string/symbol"  
  17.     />  
  18.     <EditText  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:id="@+id/numbertwo"  
  22.         android:hint="請輸入數字"  
  23.     />  
  24.     <Button  
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:id="@+id/mybutton"  
  28.         android:text="@string/calculate"  
  29.     />  
  30. </LinearLayout>  

 result.xml

 

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

 

注意:result.java需要註冊到AndroidManifest.xml文件中

 

<activity android:name=".Result" android:label="@string/result">

</activity>

 

運行結果如下:



 

 

例子二:調用系統內置action

 

 

Java代碼  收藏代碼
  1. package org.hualang.intent;  
  2. import android.app.ListActivity;  
  3. import android.content.Intent;  
  4. import android.net.Uri;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.ListView;  
  9.   
  10. public class IntentTest extends ListActivity {  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         // 菜單項數組  
  15.         String[] menus = { "查看電話信息""編輯電話信息""顯示撥打電話界面","直接打電話","訪問瀏覽器","訪問地圖"};  
  16.         // 將菜單項數組設置爲ListView的列表項展示  
  17.         setListAdapter(new ArrayAdapter<String>(this,  
  18.                 android.R.layout.simple_list_item_1, menus));  
  19.         getListView().setTextFilterEnabled(true);  
  20.     }  
  21.       
  22.     @Override  
  23.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  24.         Intent intent = new Intent();  
  25.         Uri uri ;  
  26.         String data;  
  27.         switch (position) {  
  28.         // 查看_id 爲1的用戶電話信息  
  29.         case 0:  
  30.             data = "content://contacts/people/1";  
  31.             uri = Uri.parse(data);  
  32.             intent.setAction(Intent.ACTION_VIEW);  
  33.             intent.setData(uri);  
  34.             startActivity(intent);  
  35.             break;  
  36.         // 編輯_id 爲1的用戶電話信息  
  37.         case 1:  
  38.             data = "content://contacts/people/1";  
  39.             uri = Uri.parse(data);  
  40.             intent.setAction(Intent.ACTION_EDIT);  
  41.             intent.setData(uri);  
  42.             startActivity(intent);  
  43.             break;  
  44.         // 顯示撥打電話界面  
  45.         case 2:  
  46.             data = "tel:13800138000";  
  47.             uri = Uri.parse(data);  
  48.             intent.setAction(Intent.ACTION_DIAL);  
  49.             intent.setData(uri);  
  50.             startActivity(intent);  
  51.             break;  
  52.         // 直接打電話  
  53.         case 3:  
  54.             data = "tel:13800138000";  
  55.             uri = Uri.parse(data);  
  56.             intent.setAction(Intent.ACTION_CALL);  
  57.             intent.setData(uri);  
  58.             startActivity(intent);  
  59.             break;  
  60.         // 訪問瀏覽器  
  61.         case 4:  
  62.             data = "http://www.google.com";  
  63.             uri = Uri.parse(data);  
  64.             intent.setAction(Intent.ACTION_VIEW);  
  65.             intent.setData(uri);  
  66.             startActivity(intent);  
  67.             break;  
  68.         // 訪問地圖  
  69.         case 5:  
  70.             data = "geo:39.92,116.46";  
  71.             uri = Uri.parse(data);  
  72.             intent = new Intent(Intent.ACTION_VIEW,uri);  
  73.             startActivity(intent);  
  74.             break;  
  75.         default:  
  76.             break;  
  77.         }  
  78.     }  
  79. }  

 

 main.xml

 

Java代碼  收藏代碼
  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. </LinearLayout>  

 AndroidManifest.xml

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

運行結果如下:



 當點擊直接打電話後


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章