简体   繁体   中英

Android Implicit and Explicit Intents

When use an Implicit Intent vs an Explicit Intent?

For this Implicit Intent:

Intent searchintent = new Intent()
searchintent.setAction(Intent.ACTION_VIEW) searchintent.setData(Uri.parse("http://www.google.com")) startActivity (searchintent)

How would I set another action and how would I use setData with something other than a Uri parameter?

I am just generally trying to understand both types of intents.

Thanks

Implicit intents is when you want to perform an action but you don't know what application the user currently has to handle that action. For example sending an email, there are many applications for that so the user can choose which application he wants to use.

Explicit intent is used to start an activity within your application, if you have mainactivity, and secondActivity, and you want to start the second activity you call an explicit intent.

StartActivity(new Intent(getBaseContext(), secondActivity.class));

You can pass data between activities by adding extras to the bundle that is being passed with the Intent.

Intent i = new Intent(getBaseContext, secondActivity.class);
i.putExtra("key",value);
startActivity(i);

And to get back the extras in your second activity just call:

getIntent().getStringExtra("key");

Or if you want only to get the "data" uri that was passed you can call

getIntent().getData();

The extra can be for example an int/double/String or a parcable object

http://developer.android.com/reference/android/os/Parcelable.html

You would use an implicit intent if the action you need to complete can be completes by a wide variety of applications or an application that is not yours. For example, barcode scanning, contact picking, Text Message/email etc....

Explicit intent usage should be used when you want to control the activity that handles the intent (ie. why you pass a class definition to the intent).

Intents are simply calls to other activities. Each activity handle this calls (as yours) in the following way:

1) in the OnCreate() method, the activity is looking for a valid action (as in your call "ACTION_VIEW"). If the action is valid and callable by this activity, a function in this activity is called and "answers" your request. (when called with "startActivityForResult").

2) setting data to a Intent: this is needed, if the called action needs some parameters. this parameters can be a lot of types, so it can be a String or a Integer as example. You can bundle this types of data in a "Bundle" and can put that in your Intent. The called activity with specified action and data handles all that and the thing if possible, otherwise it throws an error or what ever.

In your code example, you call activity that opens a Browser (because android system knows about how to handle "ACTION_VIEW" with a URL parameter, and its a system default behavior defined by the underlaying android system.

If you write your own activity, you can define public variables as "action" and can handle that in your OnCreate method, even with needed parameters.

  1. Explicit Intents are used to call a specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use.For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activityB and then use it to directly call it.
  2. Implicit Intents are used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use. If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type. If only one component is found, Android starts the component directly. For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.

An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM