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.
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.