Android - Intent (Start Activity, Service and Deliver Broadcast)

About

An Intent is a messaging object you can use to request an action from an app component.

There are three fundamental use-cases:

Use cases

Start an activty

  • To start an activity , pass an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.
  • To receive a result from the activity when it finishes, call startActivityForResult(). Your activity receives the result as a separate Intent object in your activity's onActivityResult() callback.
Intent intent = new Intent();
intent.setClass(getContext(), DetailActivity.class);
startActivity(intent);

Start a service

  • To start a service to perform a one-time operation (such as download a file) by passing an Intent to startService(). The Intent describes the service to start and carries any necessary data.

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

Deliver a broadcast

You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().

Type

There are two types of intents:

  • Explicit
  • Implicit

Explicit

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

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.

Filter

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>

Show a map

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

Share intent

Documentation / Reference


Powered by ComboStrap