An Intent is a messaging object you can use to request an action from an app component.
There are three fundamental use-cases:
Intent intent = new Intent();
intent.setClass(getContext(), DetailActivity.class);
startActivity(intent);
If the service is designed with a client-server interface, you can bind to the service from another component by passing an Intent to bindService().
You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().
There are two types of intents:
Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start.
For example, start a new activity in response to a user action or start a service to download a file in the background.
Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)), which provides the exact class to be run.
Intent intent = new Intent();
intent.setClass(getContext(), DetailActivity.class);
startActivity(intent);
Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.
Implicit intent are resolved through the filter applied to an activity that tells which kind of data an activity can handle.
Example for Google Maps that can handle every URI with the geo scheme.
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="geo" />
</intent-filter>
From the Common Intent, Maps Intent
Example where the location is in a preference
private void showMap() {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this.getContext());
String postalCode = sharedPref.getString(getString(R.string.pref_location_key), getString(R.string.pref_location_default));
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri geoLocation = Uri
.parse("geo:0,0?")
.buildUpon()
.appendQueryParameter("q", postalCode)
.build();
intent.setData(geoLocation);
// This test verify that the intent can be handled
if (intent.resolveActivity(getContext().getPackageManager()) != null) {
startActivity(intent);
} else {
Log.d(LOG_TAG, "Couldn't call " + location + ", no receiving apps installed!");
}
}
Share intent