Detailed explanation of Intent usage

d7d08831c54dd59dce0b2e96c41e5fc2.gif

Learn with you for lifeXi, this is Programmer Android

Recommended classic articles. By reading this article, you will gain the following knowledge points:

1. Introduction to Intent
2. Main uses of Intent
3. Intent classification
4. Implicit Intent reception filter tag
5. Introduction to PendingIntent
6. Seven attributes of Intent
7. Use ADB to debug Intent

1. Introduction to Intent

Intent It is a message passing object, mainly used for communication between components, such as startup Activity, startup Service, delivery Broadcast, etc.

The main function flow chart of Intent is as follows:

29e0752bcd6aeb2a004dd49a641802c0.jpeg

Intent main function flow chart

2. Main uses of Intent

1. Start Activity

  • startActivity()
    No return value, start directlyActivity

  • startActivityForResult()
    There is a return value, and the return value is in the onActivityResult() callback

2. Start Service

  • startService()
    One-time operation

  • bindService()
    Bound components end when the component life cycle ends

3. Send Broadcast

  • sendBroadcast()
    Ordinary out-of-order broadcast

  • sendOrderedBroadcast()
    orderly broadcast

  • sendStickyBroadcast()
    Continuously sticky broadcast

3. Intent classification

1. Display Intent

Specify the component to start by name (fully qualified class name).
For example:

Intent intentActivity = new Intent(MainActivity.this,
                            ActivityMethods.class);
                    startActivity(intentActivity);

2. Implicit Intent

Instead of specifying a specific component, you declare a general action to be performed, allowing other components in your app to handle it.
For example:

/**
     * 发送短信
     * **/
    public static void SendMms(Context context, String mmsString) {

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, mmsString);
        sendIntent.setType("text/plain");
        // sendIntent.setData(Uri.parse("smsto:"));
        // This ensures only SMS apps respond
        // 修改 Intnent 选择器Tittle
        String title = context.getResources().getString(R.string.hello_world);
        Intent chooser = Intent.createChooser(sendIntent, title);

        // 验证是否有Activity 接收
        if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(chooser);
        }
    }

4. Implicit Intent receiving filter tags

To what implicits your app can receive Intent, use  <intent-filter>the element in the manifest file to declare one or more  Intentfilters for each app component. Each Intent filter  Intentspecifies the types it accepts based on its operations, data, and categories Intent. The system will only pass this to the app component if the implicit Intent can be Intentpassed through one of the filters  Intent.

1. <action>

In  namethe properties, declare the accepted  Intentoperations. The value must be a text string value of the operation, not a class constant.

For example:
java the code startedIntentAction

Intent sendIntent = new Intent("String_action");

The filter tags in Androidmanfest.xml are as follows:

4433cef1e9356b5c6fa5fcd9627f985c.jpeg

Androidmanfest label statement

2.<data>

 Declare accepted data types using one or more properties that specify  URI aspects ( scheme、host、port、pathetc.) and  types of data.MIME

3.<category>

In  name the properties, declare the accepted Intent categories. The value must be a text string value of the operation, not a class constant.

For example:

e6d4b031a84904fc359f5141a35de7f7.jpeg

category attribute usage

4. Prevent other applications from dropping their own components through Intent

android:exported="false"

5. Application main entry point Action

<action android:name="android.intent.action.MAIN" />

6. Launcher icon entry Action

The following two elements must be paired Activityto appear in the app launcher.

eb475bee484d60df6a13738ae4a41fdb.jpeg

Launcher tab entry

7. Note:

CATEGORY_LAUNCHER The category indicates that this  Activityicon should be placed in the system's app launcher. If <activity>the element does not use  iconthe specified icon, the system will use <application>the icon in the element

5. Introduction to PendingIntent

PendingIntentObject is Intenta wrapper for objects. PendingIntent The main purpose of is to authorize external applications to use the included  Intentas if it were executed from within your application's own process.

Mainly used in the following scenarios

  • 1.Notification NotificationManager

  • 2. Apply widgets AppWidget

  • 3. Scheduled tasks AlarmManager

1. Notes on using PendingIntent:

  • 1.PendingIntent.getActivity()
    is suitable for  Activitystartup  Intent.

  • 2.PendingIntent.getService()
    is suitable for Servicestartup  Intent.

  • 3.PendingIntent.getBroadcast()
    is suitable for  BroadcastReceiver startup Intent.

6. Seven attributes of Intent

1. Component Name (full class and component name of the target component)

setComponent(),
getComponent(),
setClass() ,
setClassName()

2. Action (the action that the intent will perform)

setAction()
getAction()

3. Data (used to provide operation data to the Action property)

URIObject scheme://host:port/path (protocol header, host, port, path)

4. Type classification

Specify the corresponding type. If not specified, it will be automatically deduced based on the data Data.UriMIME

5. Category Category

To Action provide additional attachment category information, there can be more than one Category, but there must be one default.

<!-- 默认分类必须加上,否则会报错 -->
<category android:name="android.intent.category.DEFAULT"/>

6. Extra data carrier

Data is stored through key-value pairs to Actionprovide data exchange between multiple entities.

7. Flags

Mark how the component is started, how it is treated after startup, FALG_ACTIVITY_SINGLE_TOP
FALG_ACTIVITY_CLEAR_TOPetc.)

7. Use ADB to debug Intent

1. Grammar

adb shell am start -a <ACTION> -t <MIME_TYPE> -d <DATA> \
  -e <EXTRA_NAME> <EXTRA_VALUE> -n <ACTIVITY>

2.Examples

adb shell am start -a android.intent.action.DIAL \
  -d tel:555-5555 -n org.example.MyApp/.MyActivity

references:

[Tencent Documentation] Android basic knowledge base
https://docs.qq.com/doc/DSWdKRWh1VnVHYWFP

Friendly recommendation:

Collection of useful information on Android development

At this point, this article has ended. The editor thinks the article is reprinted from the Internet and is excellent. You are welcome to click to read the original article and support the original author. If there is any infringement, please contact the editor to delete it. Your suggestions and corrections are welcome. We look forward to your attention and thank you for reading, thank you!

652c65933e26333789745310a81412c0.jpeg

Click to read the original article and like the boss!

Guess you like

Origin blog.csdn.net/wjky2014/article/details/132033398