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

Card Puncher Data Processing

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





Discover More
Card Puncher Data Processing
Android

When you create an app with Studio, it will create an initial activity class that will start when the app is launched. The default name of this activity is MainActivity. Activities can contain one or...
Android Studio Activity
Android - Activity (UI Single Screen)

An activity is a plain Java class representing a single screen (known also as a presentation layer). Every Activity is invoked by an Intent, regardless of how the user navigated there. You can get the...
Android Tablet Handset Design
Android - Responsive Design (Multiple screens)

Responsive UI in Android. See also: See A modular fragment allows you to change your fragment combinations for different screen sizes. ...
Card Puncher Data Processing
Android - Service (background operation without UI interaction)

A Service is a component that performs operations in the background without a user interface. (such as download a file) You start a service by passing an intent. Default Services have a higher priority...



Share this page:
Follow us:
Task Runner