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.
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:
-
Action
-
A string naming the action to perform. Usually one of the platform-defined values such as
ACTION_SEND
orACTION_VIEW
.Specify this in your intent filter with the
<action>
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).
Data
-
A description of the data associated with the intent.
Specify this in your intent filter with the
<data>
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.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 theandroid:mimeType
attribute to declare the type of data your activity handles, such astext/plain
orimage/jpeg
.
Category
-
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
<category>
element. -
》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>
.
<activity android:name="ShareActivity"> <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>
<activity android:name="ShareActivity"> <!-- 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>》 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()
.
@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 ... } }
// 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();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.
Note: There's no need to check whether your activity was started with startActivity()
orstartActivityForResult()
. 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.