Android-allow other applications to start your activity

Source: Internet
Author: User

This article translated from: http://developer.android.com/training/basics/intents/filters.html

The previous two lessons have been focusing on one side of the story: Start the activity of another application from your application. However, if your application can perform an operation that may be useful to another application, your application should be prepared to respond to Operation requests from other applications. For example, if you create a community application that can share messages and photos with your friends, you should support intent of the action_send type, this allows you to start sharing operations from other applications and load your applications to perform this operation.

To allow other applications to start your activity, you must add an <intent-filter> element to the corresponding <activity> element of your configuration file.

When your application is installed on a device, the system recognizes your intent filter, add this information to an internal intent directory supported by all installed applications. When an application uses an implicit intent object to call the startactivity () or startactivityforresult () method, the system will find the activities in this directory that can respond to the intent requests.

Add intent Filter

In order to correctly define the intent objects that your activity can process, the operations and data received by the activity should be defined as much as possible in each added intent filter.

If the activity has an intent filter that exactly matches the following intent object conditions, the system will send the given intent object to the activity:

Action

Defines the name (string) of the operation to be executed ). It is usually a value defined by the platform, such as action_send or action_view.

Use the <action> element in the filter to specify the action. The value specified in this element must be the complete string name of the operation, rather than the API constant.

Data

Define data that matches the intent

Use the <DATA> element in the filter to specify the data. Using one or more attributes of this element, you can simply specify the MIME type, Uri prefix, Uri scheme, or combination of these, and other data types that can be received.

Note: If you do not need to declare the URI of the specified data (for example, when your activity is processing additional data of other types, rather than URI), you should only specify Android: the mimetype attribute is used to declare the data types that your activity can process, such as text/plain or image/JPEG.

Category

Provides two methods for external activities to process intent objects, which are usually related to user gestures or other locations. The system supports several different categories, but is rarely used. However, by default, all implicit intent objects must be defined using category_default.

Use the <Category> element in the intent filter to specify the category information.

In your intent filter, You can nest the corresponding XML Element in the <intent-filter> element to declare the conditions of the intent object that your activity can receive.

For example, the following activity's intent filter statement can process intent objects of the action_send type, and the data type can be text or image:

<Activityandroid: Name = "activity">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Send"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: mimetype = "text/plain"/>
<Data Android: mimetype = "image/*"/>
</Intent-filter>
</Activity>

Each input intent object can only specify one operation and one data type, however, you can declare multiple instances of <action>, <Category>, and <DATA> elements in each <intent-filter> element.

If any two operations and data behaviors are mutually exclusive, you should create an independent intent filter for them, specify the operation that can be received and the corresponding data type in each intent filter.

For example, assume that your activity can use intent of the action_send and action_sendto types to process text and images. In this case, you must define an independent intent filter for the two operations, because the intent object of the action_sendto type must use the data URI to specify the recipient's address, for example:

<Activityandroid: Name = "activity">
<! -- Filter for sending text; accepts sendto action with sms uri schemes -->
<Intent-filter>
<Action Android: Name = "android. Intent. Action. sendto"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: Scheme = "SMS"/>
<Data Android: Scheme = "smsto"/>
</Intent-filter>
<! -- Filter for sending text or images; accepts send action and text or image data -->
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Send"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: mimetype = "image/*"/>
<Data Android: mimetype = "text/plain"/>
</Intent-filter>
</Activity>

Note:To receive implicit intent objects, the intent filter must contain the category_default category. If the category_default category is included, all intent objects started by startactivity () and startactivityforresult () methods will be processed. Otherwise, the implicit intent object will not be received by your activity.

Process intent objects in your activity

To determine the operations to be performed in your activity, you can read the intent object that starts the activity.

When an activity is started, call the getintent () method to obtain the intent object that starts the activity. You can do this at any time in the lifecycle of an activity, but it should usually be done in an early return method (such as oncreate () or onstart.

For example:

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
 
Setcontentview (R. layout. Main );
 
// 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...
}
}

Returned results

If you want to return the result to the caller, simply call the setresult () method to specify the specific result code and result intent object. After the operation is complete, and to return to the original caller, call the finish () method to close (or destroy) The called activity. For example:

// 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 );
Finish ();

You must specify the result code in the returned result. Generally, it should be result_ OK or result_canceled. Then, use an intent object as needed to provide additional data.

Note: The default result is result_canceled. Therefore, if you press the return button before the operation is completed and the result is set, the initial activity will receive the result of "canceled.

If you simply return an integer to specify one of several optional results, you can set the result code to any value greater than 0. If you use the result code to send an integer without containing the intent object, you can call the setresult () method and pass only one result code to it. For example:

Setresult (result_color_red );
Finish ();

In this case, there are few possible results, so the result code can be a locally defined INTEGER (greater than 0 ). In this way, it works well when you return the result to the activity in your own application, because the activity that receives the result can reference a public constant to judge the value of the result code.

Note: you do not need to check whether your activity is started using the startactivity () method or startactivityforresult () method. If you want to return results when starting your activity, simply call the setresult () method. If the startactivityforresult () method is called to start an activity, the system will send the result you provided to the setresult () method to it. Otherwise, the result will be ignored.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.