“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

public class Intent implements Parcelable, Cloneable 
Copy the code

1. Introduction of Intent

An Intent is a serialized messaging object, implemented in Parcelable. Used for interaction and communication between components. An Intent describes an action,catogary, and data for an action in an application. Based on the description of an Intent, the system finds the corresponding component to invoke the component. There are three basic use cases:

  • Start the Activity

    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, the 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, services that contain JobScheduler can be launched.

    For versions of Android prior to 5.0 (API level 21), services can be started using methods of the Service class. By passing the Intent to startService() or bindService()

  • 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 the broadcast to other applications by passing the Intent to sendBroadcast() or sendOrderedBroadcast().

Types of Intent

Intents fall into two types:

  1. According to the Intent

We specify the fixed class in the Intent object that we construct in startActivity() or startService that we can provide the class to construct ComponentName

Public Intent(Context packageContext, Class<? > cls) { mComponent = new ComponentName(packageContext, cls); } public @NonNull Intent setClass(@NonNull Context packageContext, @NonNull Class<? > cls) { mComponent = new ComponentName(packageContext, cls); return this; Intents = intents () intent.setClass(context,class) startActivity(intent)*Copy the code
  1. An implicit Intent

IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter If multiple IntentFilters are compatible, a dialog box is displayed for users to select an application. Below is the official picture of Gu Ge

How an implicit Intent is passed through the system to start another Activity:[1]  Activity ACreates an object that contains the operation descriptionIntentAnd pass it tostartActivity().[2]Android searches all applications for Intent filters that match the Intent. Once I find a match,[3]The system calls matching activities (Activity B)onCreate()Method and pass it toIntentTo start the matching Activity.

Intents = intent.setPackage(" com.stxx.newApp ") intent.action = "com.stxx.newapp.process" // or Intent.setpackage (" com.stxx.newApp ") intent.setPackage(" com.stxx.newApp ") intent.setPackage(" com.stxx.newApp ") intent.setPackage(" com.stxx.newApp ")  mServiceConnection, Context.BIND_AUTO_CREATE)Copy the code

Considerations for using implicit IntEnts:

When using implicit intents, check to see if the intent matches, otherwise the program will crash. This can be checked using the ResolveActivity() method in the Intent. If the result is not null, proceed with startActivity() or startService()

Intent.action_send putExtra(intent.extra_text, intent.action_send, intent.extra_text, intent.action_send, intent.action_send, intent.extra_text, SQL > select * from 'manifest.xml'; SQL > select * from 'manifest.xml'; SQL > select * from 'manifest.xml' The third method does not need method 1: if (sendIntent. ResolveActivity (packageManager)! = null) {startActivity(sendIntent)} If (packageManager. QueryIntentActivities (sendIntent MATCH_DEFAULT_ONLY)) {startActivity (sendIntent)} method 3: val info = packageManager.resolveActivity(sendIntent, MATCH_DEFAULT_ONLY) if (info ! = null) {startActivity(sendIntent)} else {toast(" not matched to component ")}Copy the code

If multiple applications respond to an implicit Intent, the user can select the application to use and set it as the default option for the action. The ability to select the default option is useful if a user might want to perform an action each time using the same application (for example, when opening a Web page, users tend to prefer just one web browser).

However, if multiple applications can respond to an Intent, and the user may want to use a different application each time, the selector dialog should be displayed explicitly. The selector dialog asks the user to select the application for the operation (the user cannot select the default application for the operation). For example, when an application uses the ACTION_SEND operation to “share,” the user may need to use a different application depending on the current situation, so the selector dialog should always be used, as shown in Figure 2.

To display the selector, create the Intent using createChooser() and pass it to startActivity(), as shown in the following example. This example displays a dialog box with a list of applications that responded to the Intent passed to the createChooser() method, and uses the supplied text as the dialog title.

val sendIntent = Intent(Intent.ACTION_SEND) ... val title: String = resources.getString(R.string.chooser_title) val chooser: Intent = Intent.createChooser(sendIntent, title) if (sendIntent.resolveActivity(packageManager) ! = null) { startActivity(chooser) }Copy the code

Use startActivity () to start the implicit intent, intent will DEFAULT to add an android. The intent. The category. The DEFAULT category so attention should be paid to the following code in the intent – filter configuration

<category android:name="android.intent.category.DEFAULT"/>
Copy the code

IntentFilter Matching rule

The Intent object contains seven attributes: Action, Data, Category, Type, Component, Extra, and Flag. The most common of these are the Action and Data properties.

  • Action:

The attribute value is a string. The Intent must carry an action. Some actions are predefined, but you can also define your own actions within the application. You can declare more than one action element. The following is an example:

<intent-filter>
    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.VIEW" />
    ...
</intent-filter>
Copy the code

The Intent must specify an action otherwise it cannot match any component. An action specified in an Intent must match one of the intent-filter entries

  • category

An Intent filter can declare neither any

element nor multiple such elements. If set the intent – filter must be set the category of the android: name = “android. Intent. The category. The DEFAULT”, “the reason it has already been said. Also, the category set in the intent must match one of the intent-filters. If the Intent does not contain any category, only the action is matched.

  • Data

Each element can specify a URI structure and a data type (MIME media type). Each part of the URI is a separate attribute: Scheme, host, port, and path:

<scheme>://<host>:<port>/<path>

Filtering rules:

  • If data is defined in an intent-filter, the intent must also carry matching data. If data is not defined, the intent cannot carry matching data
  • If an intent-filter does not specify a mimeType, the intent cannot specify a mimeType
  • If an intent-filter lists the same MimeType and does not specify a URI format, an intent that contains the MimeType but does not contain the URI will pass.

Note that setDataAndType() is used when both data and type are set in an Intent. Because each method is called separately, another parameter is set to NULL

public @NonNull Intent setData(@Nullable Uri data) {
    mData = data;
    mType = null;
    return this;
}
Copy the code

Here is an example of opening the activity in a browser: two filters are declared. You only need one hit to start the activity

<activity android:name=".view.start.LaunchActivity" android:launchMode="singleTop" android:screenOrientation="portrait" android:theme="@style/AppStartLoadTranslucent"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category The android: name = "android. Intent. The category. The DEFAULT" / > / / representative can be opened from the browser < category The android: name = "android. Intent. Category. BROWSABLE" / > / / declare the scheme for xjtmeeting; <data Android: Scheme ="xjtmeeting" /> </intent-filter>Copy the code