本文總結講述了Android常用的intent action功能。分享給大家供大家參考,具體如下:
Android基本的設計理念是鼓勵減少組件間的耦合,因此Android提供了Intent (意圖) ,Intent提供了一種通用的訊息系統,它允許在你的應用程式與其它的應用程式間傳遞Intent來執行動作和產生事件。Intent作為聯絡各Activity之間的紐帶,其作用並不僅僅只限於簡單的資料傳遞。通過其內建的屬性,其實可以方便的完成很多較為複雜的操作。例如直接調用撥號功能、處理接收簡訊,諸如此類,都可以通過設定Intent屬性來完成。
Intent主要有以下四個重要屬性,它們分別為:
Action:Action屬性的值為一個字串,它代表了系統中已經定義了一系列常用的動作。通過setAction()方法或在資訊清單檔AndroidManifest.xml中設定。標識Activity為一個程式開始的範例程式碼(AndroidManifest.xml進行配置)如下:
<span style="font-size:16px;"><intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /></intent-filter></span>
Data:Data通常是URI格式定義的操作資料。例如:tel:// 。通過setData()方法設定。
Category:Category屬性用於指定當前動作(Action)被執行的環境。通過addCategory()方法或在資訊清單檔AndroidManifest.xml中設定。預設為:CATEGORY_DEFAULT。
Extras:Extras屬性主要用於傳遞目標組件所需要的額外的資料。通過putExtras()方法設定。
在本文中,主要介紹常見action的使用,Action描述Intent所觸發動作名字的字串,對於BroadcastIntent來說,Action指被廣播出去的動作。理論上Action可 以為任何字串,而與Android系統應用有關的Action字串以靜態字串常量的形式定義在了Intent類中。Action中包含很多種,例如呼入,呼出電話,老師上課講的接受簡訊等等,下面謹對常見的與系統有關的action進行整理:
1. Intent.ACTION_MAIN
String: android.intent.action.MAIN
標識Activity為一個程式的開始。
2. Intent.Action_CALL
Stirng: android.intent.action.CALL
呼叫指定的電話號碼。
Intent intent=new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:10086");startActivity(intent);
3. Intent.ACTION_POWER_CONNECTED;
插上外部電源時發出的廣播
4 Intent.ACTION_POWER_DISCONNECTED;
已斷開外部電源串連時發出的廣播
5.Intent.Action.DIAL
String: action.intent.action.DIAL
調用撥號面板
Intent intent=new Intent();intent.setAction(Intent.ACTION_DIAL);intent.setData(Uri.parse("tel:10086");startActivity(intent);
6.Intent.Action.ALL_APPS
String: andriod.intent.action.ALL_APPS
列出所有的應用。
7.Intent.ACTION_ANSWER
Stirng:android.intent.action.ANSWER
處理呼入的電話。
8 .Intent.ACTION_BUG_REPORT
String: android.intent.action.BUG_REPORT
顯示Dug報告。
9. Intent.Action_CALL_BUTTON
String: android.action.intent.CALL_BUTTON.
相當於按“撥號”鍵。
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);startActivity(intent);
10. Telephony.SMS_RECEIVED
String: android.provider.Telephony.SMS_RECEIVED
接收簡訊的action
<intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> <data android:host="localhost"/></intent-filter>
11. Intent.ACTION_GET_CONTENT
String: android.intent.action.GET_CONTENT
允許使用者選擇特殊種類的資料,並返回(特殊種類的資料:照一張相片或錄一段音)
12. Intent.ACTION_BATTERY_LOW;
String: android.intent.action.BATTERY_LOW
表示電池電量低
13. Intent.ACTION_SEND
String: android.intent.action.Send
發送郵件的action
14. Intent.ACTION_CALL_PRIVILEGED
String:android.intent.action.CALL_PRIVILEGED
調用skype的action
Intent intent = newIntent("android.intent.action.CALL_PRIVILEGED");intent.setClassName("com.skype.raider","com.skype.raider.Main");intent.setData(Uri.parse("tel:" + phone));startActivity(intent);
15. Intent.ACTION_CLOSE_SYSTEM_DIALOGS
當螢幕逾時進行鎖屏時,當使用者按下電源開關,長按或短按(不管有沒跳出話框),進行鎖屏時,android系統都會廣播此Action訊息
以上是對常見的action進行總結,action其實有很多,如果要使用上文沒有列舉到的,google即可。
更多關於Android相關內容感興趣的讀者可查看本站專題:《Android編程之activity操作技巧總結》、《Android資源操作技巧匯總》、《Android檔案操作技巧匯總》、《Android操作SQLite資料庫技巧總結》、《Android操作json格式資料技巧總結》、《Android資料庫操作技巧總結》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》、《Android視圖View技巧總結》及《Android控制項用法總結》
希望本文所述對大家Android程式設計有所協助。