Android培训翻译_允许其他应用程序启动你的Activity
This lesson teaches you to
添加一個意圖過濾器
在你的Activity中處理Intent
返回結果
You should also read
- Sharing Content
共享內容
The previous two lessons focused on one side of the story: starting another app's activity from your app. But if your app can perform an action that might be useful to another app, your app should be prepared to respond to action requests from other apps. For instance, if you build a social app that can share messages or photos with the user's friends, it's in your best interest to support the ACTION_SEND intent so users can initiate a "share" action from another app and launch your app to perform the action.
前兩節可專注于一件事情:從你的程序啟動其他程序的activity。但是如果你的程序可以執行一個能對其它程序有用的動作,你的程序應該準備響應其它程序動作的請求。舉個例子,如果你構建了一個社交程序可以讓用戶和朋友分享信息或照片,支持 ACTION_SEND 意圖對你最為有利,這樣用戶可以從別的程序發起個“共享”動作,并啟動你的程序來執行該動作。
?
To allow other apps to start your activity, you need to add an <intent-filter>element in your manifest file for the corresponding <activity> element.
為了讓別的程序啟動你的Activity,你需要添加一個<intent-filter>元素到manifest文件相應的<activity>元素下。
?
When your app is installed on a device, the system identifies your intent filters and adds the information to an internal catalog of intents supported by all installed apps. When an app calls startActivity() or startActivityForResult(), with an implicit intent, the system finds which activity (or activities) can respond to the intent.
你的程序安裝到一個設備上時,系統識別出你的意圖過濾器并把這個信息添加到意圖的一個內部目錄,這些意圖被所有已安裝程序支持。當一個程序用一個隱式意圖調用 startActivity() 或者 startActivityFrorResult()時,系統分辨哪(幾)個activy可以響應該意圖。
?
Add an Intent Filter 添加一個意圖過濾器
In order to properly define which intents your activity can handle, each intent filter you add should be as specific as possible in terms of the type of action and data the activity accepts.
為了正確定義哪些意圖可以被你的activity處理,每個你添加的意圖過濾器應當依據動作的類型和actvitiy接受的數據盡可能的具體。
?
The system may send a given Intent to an activity if that activity has an intent filter fulfills the following criteria of the Intent object:
系統可能會發送給定的Intent到一個activity ,如果那個 activity 有一個意圖過濾器滿足下面 Intent 對象的標準:
一個字符串命名的動作執行。通常是平臺定義的一個值,例如:ACTION_SEND 或者 ACTION_VIEW。
用 <action> 標簽在你的意圖過濾器中指定。這個標簽中指定的值必須是動作的全字符串名,而不是API常數(見下面的例子)
與意圖關聯的數據的描述。
用<data>標簽在你的意圖過濾器中指定。這個標簽使用一到多個屬性,你可以指定MIME類型,URI 前綴,URI 計劃,或者這些和其它可用數據類型的組合。
?
Note: If you don't need to declare specifics about the dataUri (such as when your activity handles to other kind of "extra" data, instead of a URI), you should specify only the android:mimeType attribute to declare the type of data your activity handles, such as text/plain or image/jpeg.
注:如果你不需要聲明有關數據Uri的細節(例如,你activity處理其它類型的extra數據,而不是URI時),你應當唯一指明 android:mimeType 屬性聲明你activity處理的數據類型,例如 text/plain 或者 image/jpeg。
?
提供一個額外的方法描述activity處理意圖的特征, 常常與用戶手勢或其啟動的位置有關。系統支持幾種不同的類別,但是大多數很少使用。然而,所有的隱式意圖默認都是用CATEGORY_DEFAULT定義的。
用<category>標簽在你的意圖過濾器中指定。
In your intent filter, you can declare which criteria your activity accepts by declaring each of them with corresponding XML elements nested in the <intent-filter>element.
在你的意圖過濾器中,你可以聲明你的activity 接受的標準,在<intent-filter> 標簽中聲明每個對象相應的xml元素。
?
For example, here's an activity with an intent filter that handles the ACTION_SEND intent when the data type is either text or an image:
例如下面的activity,當數據類型為文本或圖像時,使用意圖過濾器處理 ACTION_SEND 意圖
?
Each incoming intent specifies only one action and one data type, but it's OK to declare multiple instances of the <action>, <category>, and <data> elements in each<intent-filter>.
每個傳入的意圖僅指定一個動作和一個數據類型,但是每個<intent-filter>中聲明多個<action>,<category>和<date>元素也是沒問題的。
?
If any two pairs of action and data are mutually exclusive in their behaviors, you should create separate intent filters to specify which actions are acceptable when paired with which data types.
如果任意兩對動作和數據在他們的行為中是相互排斥的,你應當創建獨立的意圖過濾器去指定那些動作可以接受與哪些數據類型配對。
?
For example, suppose your activity handles both text and images for both the ACTION_SEND and ACTION_SENDTO intents. In this case, you must define two separate intent filters for the two actions because a ACTION_SENDTO intent must use the data Uri to specify the recipient's address using the send or sendto URI scheme. For example:
假設你的activity 為ACION_SEND和ACTION_SENDTO 意圖處理文字和圖像。這個例子中,你必須單獨為兩個動作定義兩個意圖過濾器,因為一個ACTION_SENDTO意圖必須通過send或者sendto URI計劃使用Uri數據指定接收者的地址。如下:
?
Note: In order to receive implicit intents, you must include theCATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they contained the CATEGORY_DEFAULT category. If you do not declare it, no implicit intents will resolve to your activity.
注:為了獲取隱式意圖,你必須在意圖過濾器中包含CATEGORY_DEFAULT 類別。startActivity()和startActivityForResult()方法對待所有意圖就像他們包含了CATEGORY_DEFAULT類別一樣。如果你不想聲明它,你的activity將檢索不到任何的隱式意圖。
?
For more information about sending and receiving ACTION_SENDintents that perform social sharing behaviors, see the lesson about Receiving Content from Other Apps.
更多關于發送和接收 ACTION_SEND 意圖執行社交分享行為的信息,請參見課程 從其它程序接收內容。
?
Handle the Intent in Your Activity
在你的Activity中處理意圖
In order to decide what action to take in your activity, you can read the Intent that was used to start it.
為了決定在你的activty中獲得什么動作,你可以閱讀Intent,用來啟動activity。
?
As your activity starts, call getIntent() to retrieve theIntent that started the activity. You can do so at any time during the lifecycle of the activity, but you should generally do so during early callbacks such asonCreate() or onStart().
因為你activity啟動,調用getIntent()來獲取啟動該activity的意圖。你可以在該activity聲明周期的任意時刻這么做,但是你應該通常在回調的早起這么做,例如 onCreate() 或者 onStart()。
?
For example: 示例
@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// Get the intent that started this activity // 獲取啟動了該activity的意圖Intent intent = getIntent();Uri data = intent.getData();// Figure out what to do based on the intent type // 弄明白基于這個意圖類型可以做什么if (intent.getType().indexOf("image/") != -1) {// Handle intents with image data ... // 用圖像數據處理意圖...} else if (intent.getType().equals("text/plain")) {// Handle intents with text ... // 用文本處理意圖... } }?
Return a Result
返回結果
If you want to return a result to the activity that invoked yours, simply call setResult() to specify the result code and result Intent. When your operation is done and the user should return to the original activity, call finish() to close (and destroy) your activity. For example:
如果你想向調用你的activity返回結果,只需調用 setResult() 去指定結果代碼和結果意圖。當你的操作完成并且用戶想要返回之前的activity時,調用 finish() 關閉(并摧毀)你的activity。例如:
?
You must always specify a result code with the result. Generally, it's either RESULT_OK or RESULT_CANCELED. You can then provide additional data with an Intent, as necessary.
你必須總是指定結果的結果代碼。一般來講,是RESULT_OK 或是RESULT_CANCELED。有必要的話,你之后可以用意圖提供額外的數據。
?
Note: The result is set to RESULT_CANCELED by default. So, if the user presses the Backbutton before completing the action and before you set the result, the original activity receives the "canceled" result.
注:默認情況下結果被設置為 RESULT_CANCELED。因此,如果用戶在完成動作和你設置結果前按了回退按鈕,原先的activity會獲得“被取消”結果。
?
If you simply need to return an integer that indicates one of several result options, you can set the result code to any value higher than 0. If you use the result code to deliver an integer and you have no need to include the Intent, you can call setResult() and pass only a result code. For example:
如果你只是需要返回一個整數,指出幾種結果選項之一,你可以設置返回碼為任何大于0的值。如果你使用結果代碼傳送一個整數而不需要包括意圖,你可以調用 setResult() 僅傳送一個結果代碼。
?
In this case, there might be only a handful of possible results, so the result code is a locally defined integer (greater than 0). This works well when you're returning a result to an activity in your own app, because the activity that receives the result can reference the public constant to determine the value of the result code.
這個例子中,可能只有幾個可能的結果,因此返回碼是個本地定義的整數(大于0)。你在自己的應用中返回一個結果到一個activity時這種方式很管用,因為獲取結果的activity可以引用公共常量來決定結果碼的值。
?
Note: There's no need to check whether your activity was started with startActivity() or startActivityForResult(). Simply call setResult() if the intent that started your activity might expect a result. If the originating activity had called startActivityForResult(), then the system delivers it the result you supply to setResult(); otherwise, the result is ignored.
注:沒必要去檢查你的activity是用 startActivity() 還是 startActivityForResult() 啟動的。 如果啟動你activity的意圖可能期待一個結果,簡單調用setResult()便好。如果原始的activity已經調用了 startActivityForResult() ,那么系統會傳送你提供的結果到setResult(),否則結果就會被忽略。
轉載于:https://www.cnblogs.com/stonesea/archive/2012/08/21/allowing-other-apps-to-start-your-activity.html
總結
以上是生活随笔為你收集整理的Android培训翻译_允许其他应用程序启动你的Activity的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 异步编程模式学习
- 下一篇: Android学习笔记(1)