Example explicit intent
An explicit intent is one that you use to launch a specific app component, such asa particular activity or service in your app. To create an explicit intent, definethe component name for the Intent
object—allother intent properties are optional.
For example, if you built a service in your app, named DownloadService
,designed to download a file from the web, you can start it with the following code:
// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse
(fileUrl));
startService(downloadIntent);
The Intent(Context, Class)
constructor supplies the app Context
and thecomponent a Class
object. As such,this intent explicitly starts the DownloadService
class in the app.
For more information about building and starting a service, see theServices guide.
Example implicit intent
An implicit intent specifies an action that can invoke any app on the device ableto perform the action. Using an implicit intent is useful when your app cannot perform theaction, but other apps probably can and you'd like the user to pick which app to use.
For example, if you have content you want the user to share with other people, create an intentwith the ACTION_SEND
actionand add extras that specify the content to share. When you callstartActivity()
with that intent, the user canpick an app through which to share the content.
Caution: It's possible that a user won't have anyapps that handle the implicit intent you send to startActivity()
. If that happens, the call will fail and your app will crash. To verifythat an activity will receive the intent, call resolveActivity()
on your Intent
object. If the result is non-null,then there is at least one app that can handle the intent and it's safe to callstartActivity()
. If the result is null,you should not use the intent and, if possible, you should disable the feature that issuesthe intent.
// Create the text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType(HTTP.PLAIN_TEXT_TYPE
); // "text/plain" MIME type
// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendIntent);
}
Note: In this case, a URI is not used, but the intent's data typeis declared to specify the content carried by the extras.
When startActivity()
is called, the systemexamines all of the installed apps to determine which ones can handle this kind of intent (anintent with the ACTION_SEND
action and that carries "text/plain"data). If there's only one app that can handle it, that app opens immediately and is given theintent. If multiple activities accept the intent, the systemdisplays a dialog so the user can pick which app to use..
参考:http://wear.techbrood.com/guide/components/intents-filters.html