Google Android开发者文档系列-与其他应用程序交互之允许其他应用启动你的Activity

标签: 谷歌Googleandroid安卓文档
337人阅读 评论(0) 收藏 举报

Allowing Other Apps to Start Your Activity(允许其他应用程序启动你的活动)


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.
之前的两次课关注应用程序交互的一面:从你的应用程序启动另一个应用程序。但是,如果你的应用程序能够执行一个可能对另一个应用程序有用的action(行为、操作),那么你的应用程序就应该准备好去响应其他应用程序发出的action(行为、操作)请求。比如,如果你创建了一个能够向用户朋友分享消息或者照片的社交应用程序,那么如果你的应用程序支持ACTION_SEND intent(意图)从而当用户从其他应用程序启动分享操作时会启动你的应用程序来执行这项操作,这样对你的应用程序来说是十分有利的。

To allow other apps to start your activity, you need to add an element in your manifest file for the corresponding element.
为了允许其他应用程序启动你的活动界面,你需要在你的项目清单文件中相应的 要素中添加要素。

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.

Add an Intent Filter(添加一个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.
为了正确的定义你的活动界面可以操做哪些Intents(意图),你添加的每个intent filter都应该将其对应的action的类型和活动界面接受的data类型定义的尽可能具体、详细。

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给一个活动界面,只要那个活动界面有一个intent filter满足Intent对象的下列标准:


A string naming the action to perform. Usually one of the platform-defined values such as ACTION_SEND or ACTION_VIEW.
Specify this in your intent filter with the element. The value you specify in this element must be the full string name for the action, instead of the API constant (see the examples below).
一个命名action操作的字符串,通常是平台定义好的值之一,例如 ACTION_SEND 或者ACTION_VIEW。
在你的intent filter中的元素下定义这个元素。你定义的该元素中action参数的值必须是action的完整的字符串名称,而不是API常量(看下面的例子)。


A description of the data associated with the intent.
Specify this in your intent filter with the element. Using one or more attributes in this element, you can specify just the MIME type, just a URI prefix, just a URI scheme, or a combination of these and others that indicate the data type accepted.
在你的intent filter的要素下定义这个参数。你可以在这个要素中使用一个或者多个属性,你可以只定义MIME类型、一个URI前缀、一个URI结构,或者一个它们和其他属性之间的可以表明可接受数据类型的组合。

Note: If you don’t need to declare specifics about the data Uri (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数据的细节(比如什么时候你的活动界面操作其他类型的额外数据,而不是URI),你应该只指定android:MIME类型属性来声明你的活动界面能够处理的数据,例如text / plain或image/ JPEG类型。*


Provides an additional way to characterize the activity handling the intent, usually related to the user gesture or location from which it’s started. There are several different categories supported by the system, but most are rarely used. However, all implicit intents are defined with CATEGORY_DEFAULT by default.
Specify this in your intent filter with the element.
在你的intent filter的 要素下定义这个元素。

In your intent filter, you can declare which criteria your activity accepts by declaring each of them with corresponding XML elements nested in the element.
在你的intent filter中,你能够在要素下分别嵌套他们的XML要素,来定义你的活动界面接受intent的标准

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:
例如,下面是一个定义了intent filter来指明操作数据类型是文本或图像的ACTION_SEND意图的Activity:

<activity android:name="ShareActivity">
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
        <data android:mimeType="image/*"/>

Each incoming intent specifies only one action and one data type, but it’s OK to declare multiple instances of the , , and elements in each .
每个将来的意图只规定了一个动作和一个数据类型,但是可以在每个要素下定义多个、和 要素的实体。

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.
如果有任何两对action和data的行为是相互排斥的,那么您应该创建单独的intent filter来指定与数据类型配的哪些动作是可以接受的。

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同时处理ACTION_SEND和ACTION_SENDTO的文本和图像数据类型的意图。在这种情况下,您必须为这两个动作定义两个独立的intent filter,因为ACTION_SENDTO意图在使用send或者sendto URI方案时必须使用Uri数据来指定收件人的地址。 例如:

<activity android:name="ShareActivity">
    <!-- filter for sending text; accepts SENDTO action with sms URI schemes -->
    //发送文本的的filter,可以接受包含sms URI方案数据类型的sendto动作的action
        <action android:name="android.intent.action.SENDTO"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="sms" />
        <data android:scheme="smsto" />
    <!-- filter for sending text or images; accepts SEND action and text or image data -->
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="image/*"/>
        <data android:mimeType="text/plain"/>

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.
注:为了接收隐式Intent,你必须在Intent filter中定义CATEGORY_DEFAULT 的category 要素。startActivity() 和startActivityForResult()方法只会处理定义了 CATEGORY_DEFAULT 的category 要素的Intent。如果你没有在你的Intent filter中定义这个要素,那么你的Activity将不会解析到任何隐式intent。

For more information about sending and receiving ACTION_SEND intents that perform social sharing behaviors, see the lesson about Receiving Simple Data from Other Apps.
想要了解更多关于发送和接收ACTION_SEND类的社会分享行为的intent相关的知识,查看Receiving Simple Data from Other Apps(从其他应用中接收简易数据)课程。

Handle the Intent in Your Activity(在你的Activity中操作intent)

In order to decide what action to take in your activity, you can read the Intent that was used to start it.

As your activity starts, call getIntent() to retrieve the Intent 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 as onCreate() or onStart().

For example:

protected void onCreate(Bundle savedInstanceState) {
    // Get the intent that started this 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的Activity返回一个结果,只需要调用setResult()方法来定义 result code和返回的intent。当你将这些操作完成后,用户应该返回原先的Activity界面,所以调用finish()方法来关闭(并销毁)你的Activity。比如:

// Create intent to deliver some kind of result data
Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri");
setResult(Activity.RESULT_OK, result);

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 code,通常情况下,它的值是RESULT_OK 或者RESULT_CANCELED。然后提供一个包含额外数据的intent,如果必要的话。

Note: The result is set to RESULT_CANCELED by default. So, if the user presses the Back button before completing the action and before you set the result, the original activity receives the “canceled” result.

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:
如果你只需要返回一个整数,来指示几个结果选项之一,那么你可以将result code设置为任意高于0的值。如果你使用result code发送一个整数,并且你并不需要返回结果中包含intent,那么你可以调用setResult()方法,并且只传递一个result code。 例如:


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.
在这种情况下,result code只可能是一个本地定义的整数(大于0)。当你向自己应用中的Activity返回结果时,这种方式很好用,因为接收结果的Activity可以引用公共变量来决定result code的值。

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.


* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    • 访问:27321次
    • 积分:520
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:61篇
    • 译文:1篇
    • 评论:2条