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:
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 operationbindService()
Bound components end when the component life cycle ends
3. Send Broadcast
sendBroadcast()
Ordinary out-of-order broadcastsendOrderedBroadcast()
orderly broadcastsendStickyBroadcast()
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 Intent
filters for each app component. Each Intent
filter Intent
specifies 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 Intent
passed through one of the filters Intent
.
1. <action>
In name
the properties, declare the accepted Intent
operations. The value must be a text string value of the operation, not a class constant.
For example: java
the code startedIntent
Action
Intent sendIntent = new Intent("String_action");
The filter tags in Androidmanfest.xml are as follows:
Androidmanfest label statement
2.<data>
Declare accepted data types using one or more properties that specify URI
aspects ( scheme、host、port、path
etc.) 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:
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 Activity
to appear in the app launcher.
Launcher tab entry
7. Note:
CATEGORY_LAUNCHER
The category indicates that this Activity
icon should be placed in the system's app launcher. If <activity>
the element does not use icon
the specified icon, the system will use <application>
the icon in the element
5. Introduction to PendingIntent
PendingIntent
Object is Intent
a wrapper for objects. PendingIntent
The main purpose of is to authorize external applications to use the included Intent
as 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 forActivity
startupIntent
.2.PendingIntent.getService()
is suitable forService
startupIntent
.3.PendingIntent.getBroadcast()
is suitable forBroadcastReceiver
startupIntent
.
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)
URI
Object 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
.Uri
MIME
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 Action
provide 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_TOP
etc.)
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!
Click to read the original article and like the boss!