Android Learning Road (9) Intent

An Intent is a messaging object that you can use to request actions from other application components. Although Intent can facilitate communication between components in many ways, its basic use cases mainly include the following three:

  • Start Activity

An Activity represents a screen in an application. You start a new Activity instance by passing an Intent to startActivity(). Intent is used to describe the Activity to be started and carry any necessary data.
Call startActivityForResult() if you want to receive results after the Activity completes. In your Activity's onActivityResult() callback, your Activity receives the result as a separate Intent object. See the Activity guide for details.

  • start 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 higher, you can start a service that includes a JobScheduler. To learn more about JobScheduler, see its API-reference documentation.
Prior to Android 5.0 (API level 21), you could use methods of the Service class to start a service. You can start a service to perform a one-time operation (for example, download a file) by passing an Intent to startService(). Intents are used to describe the service to be started and carry any necessary data.
If the service is intended to use a client-server interface, you can bind to it from other components by passing an Intent to bindService(). See the Service Guide for details.

  • pass broadcast

Broadcasts are messages that any application can receive. The system will deliver various broadcasts for system events such as when the system starts up or when the device starts charging. You can pass broadcasts to other applications by passing an Intent to sendBroadcast() or sendOrderedBroadcast().

Intent type

Intents are divided into two types:

  • Explicit Intent: Specify the application that can handle the intent by providing the target application's package name or fully qualified component class name. Typically, you'll use an explicit Intent to start a component in your application because you know the class name of the Activity or service you want to start. For example, you might start a new activity within your app in response to a user action, or start a service to download a file in the background.
  • Implicit Intent: does not specify a specific component, but declares a general action to be performed, allowing components in other applications to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to ask another app that does this to show the specified location on the map.

The following diagram shows how to use an Intent when starting an Activity. When an Intent object explicitly names a specific Activity component, the system starts that component immediately.

How implicit Intents are passed through the system to start other Activities: [1] Activity A creates an Intent containing a description of the action and passes it to startActivity(). [2] The Android system searches all applications for Intent filters that match the Intent. After finding a match,[3] the system starts the matching Activity (Activity B) by calling its onCreate() method and passing it an Intent.

When using implicit intents, the Android system finds the appropriate component to launch by comparing the contents of the intent with intent filters declared in the manifest files of other applications on the device. If the intent matches the intent filter, the system starts the component and passes it the intent object. If multiple intent filters are compatible, a dialog is displayed allowing the user to choose which app to use.

An intent filter is an expression in an application's manifest file that specifies the types of intents that this component is to receive. For example, by declaring an intent filter for an activity, you can enable other applications to launch the activity directly with a specific type of intent. Likewise, if you don't declare any intent filters for your activity, the activity can only be started with an explicit intent.

Note: To ensure application security, always use an explicit Intent when starting a Service, and do not declare an Intent filter for the service. Using implicit intents to start services has security implications because you cannot be sure which services will respond to the intent, and the user cannot see which services have been started. Beginning with Android 5.0 (API level 21), if bindService() is called with an implicit Intent, the system throws an exception.

Seven properties of Intent

1) ComponentName (component name)

2) Action (action)

3) Category (category)

4) Data (data), Type (MIME type)

5) Extras (extra)

6) Flags (flag)

Guess you like

Origin blog.csdn.net/qq_32907491/article/details/132439704