This is the 21st day of my participation in the August Text Challenge.More challenges in August

An overview of the

An Intent is a messaging object that can be used to request actions from other application components. Although intents can facilitate communication between components in a number of ways, their basic use cases consist of the following three:

  • Start the Activity

    An Activity represents a screen in an application. You can start a new Activity instance by passing the Intent to startActivity(). Intents are used to describe the Activity to start and carry any necessary data.

    Call startActivityForResult() if you want to receive the result after the Activity completes. In the Activity’s onActivityResult() callback, your Activity receives the result as a separate Intent object.

  • Start the Service

    A Service is a component that performs operations in the background without using a user interface. With Android 5.0 (API level 21) and later, you can start services that include JobScheduler. For more information about JobScheduler, see its API-Reference documentation.

    For versions of Android prior to 5.0 (API level 21), you can use methods of the Service class to start services. By passing the Intent to startService(), you can start the service to perform a one-time operation (for example, download a file). Intents are used to describe the service to start and carry any necessary data.

    If the service is intended to use a client-server interface, you can bind to the service from other components by passing the Intent to bindService().

When starting a Service, always use an explicit Intent and do not declare an Intent filter for the Service. Starting a service with an implicit Intent is a security risk because you can’t be sure which services will respond to the Intent, and users can’t see which services have been started. Starting with Android 5.0 (API level 21), if you call bindService() with an implicit Intent, the system throws an exception.

  • Transfer broadcasting

    A broadcast is a message that any application can receive. The system will deliver various broadcasts for system events, such as when the system starts up or the device starts charging. You can pass broadcasts to other applications by passing the Intent to sendBroadcast() or sendOrderedBroadcast().

Build the Intent

Intent objects carry information that the Android system uses to determine which component to launch (for example, the exact name of the component or the type of component that should receive the Intent), as well as information that the recipient component uses to properly perform the action (for example, the action to take and the data to process).

An explicit Intent example

An explicit Intent is an Intent used to launch a specific application component (for example, a specific Activity or service within an application). For example, if you build a service in your application called DownloadService that aims to download files from a Web page, you can start the service using the following code:

Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);
Copy the code

An implicit Intent example

An implicit Intent specifies an action that can invoke any application on a device that can perform the action. Using implicit intEnts is useful if your application cannot perform this action but other applications can, and you want the user to select the application to use.

For example, if you want users to share your content with others, create an Intent using the ACTION_SEND action and add extra to specify the shared content. When startActivity() is called with this Intent, the user can select the application in which the content is shared.

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType("text/plain");

// Verify that the intent will resolve to an activity
if(sendIntent.resolveActivity(getPackageManager()) ! =null) {
    startActivity(sendIntent);
}
Copy the code