Intent is an important medium for communication between applications, and Activity, Service, and broadcastreceiver are all dependent on intent for booting.
The intent object roughly contains the seven attributes of component, Action, Category, Data, Type, Extra, and flag.
Component: A target component used to determine the start of a specified requirement
Action: Represents an abstract action to be performed by the intent
Category: Used to add additional categories of information to an action, usually using the action in combination with category
Data: Typically used to provide operations to the Action property
Type: Used to specify the MIME type that corresponds to the URI specified by the data
Extra: Typically used to exchange data between multiple activity
Flag: Used to add some additional control flags to the intent, intent can call the Addflags () method to add a control flag to the intent.
The following describes the effects of the properties of the intent object:
1.Component Properties
The component property of intent needs to accept a ComponentName object
// 创建一个ComponentName对象new ComponentName(MainActivity.this, TwoActivity.classnew Intent();// 为Intent设置Component属性intent.setComponent(comp);// Intent intent = new Intent(MainActivity.this, TwoActivity.class);startActivity(intent);
The following shows the package name, class name, and the component property of the activity corresponding to intent.
// 获取该Activity对应的Intent的Component属性ComponentName comp = getIntent().getComponent();// 显示该ComponentName对象的包名、类名show.setText("组件包名为:" + comp.getPackageName"\n组件类名为:" + comp.getClassName());// 运行结果:组件包名为:com.gc.component// 组件类名为:com.gc.component.TwoActivity
2.Action, category attribute, and Intent-filter configuration
Android provides the standard Action:Intent.ACTION_VIEW (represents an abstract view operation), specifically view what, Which activity is started to see the Action_view is included in the Intent-filter configuration that depends on the activity, and the activity is likely to be started.
1) Let's implement the Click button for intent specify the action jump Twoactivity, as follows:
// 创建Intent对象new Intent();// 为Intent设置Action属性(属性值就是一个普通字符串)intent.setAction("com.gc.intent.action.CRAZYIT_ACTION");startActivity(intent);
Next, configure the Intent-filter element (the intent to configure the activity response), and the Intent-filter element typically includes the action, Category, and data child elements
<activity android:name=". Twoactivity " android:label=" @string/app_name "> <intent-filter> <!--Specifies that the activity responds to the intent of the specified string, which is the action <action android:name="Com.gc.intent.action.CRAZYIT_ACTION" /> <!--Specifies that the activity responds to intent with the Action property HelloWorld-- <action android:name="HelloWorld" /> <!--Specifies that the action responds to the Category property intent the specified string- -- <category android:name="Android.intent.category.DEFAULT" /> </intent-filter></activity>
Next we get the category property of the activity corresponding to the intent in the open twoactivity
Set<String> cates = getIntent().getCategories();
2) Specify action, category invoke system activity
Android provides a number of standard action, category constants, which are the standard action constants used to start activity and the corresponding strings as follows:
| Action Constant |
corresponding String |
Simple Description |
| Action_main |
Android.intent.action.MAIN |
Application Portal |
| Action_view |
Android.intent.action.VIEW |
Display specified data |
| Action_chooser |
Android.intent.action.CHOOSER |
Show an activity selector |
| Action_get_content |
Android.intent.action.GET_CONTENT |
Let the user select the data and return the selected data |
| Action_dial |
Android.intent.action.DIAL |
Display Dial-up panel |
| Action_call |
Android.intent.action.CALL |
Make a call directly to a designated user |
| Action_search |
Android.intent.action.SEARCH |
Perform a search |
The standard category constants and their corresponding strings are as follows:
| Category Constants |
corresponding String |
Simple Description |
| Category_default |
Android.intent.category.DEFAULT |
Default Category |
| Category_browsable |
Android.intent.category.BROWSABLE |
Specifies that the activity can be safely called by the browser |
| Category_tab |
Android.intent.category.TAB |
Specify Activity as the tab page for tabactivity |
| Category_launcher |
Android.intent.category.LAUNCHER |
Activity is displayed in the top-level programs list |
| Category_info |
Android.intent.category.INFO |
Used to provide package information |
| Category_home |
Android.intent.category.HOME |
Set this activity to run with system boot |
| Category_car_dock |
Android.intent.category.CAR_DOCK |
Specifies that the activity is run when the phone is plugged into the car hardware |
| Category_desk_dock |
Android.intent.category.DESK_DOCK |
Specifies that the activity is run when the phone is plugged into the desktop hardware |
| Category_car_mode |
Android.intent.category.CAR_MODE |
Specifies that the activity can be used in a vehicle-mounted environment |
3) We set a button, click will return to the system Home screen, the button click event is as follows:
// 创建Intent对象Intent intent = new Intent();// 为Intent设置Action、Category属性intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME);startActivity(intent);
3.Data, type property
Data is typically used to provide the Action property with the information that specifies the MIME type for the URI specified by that data.
1) The data and type relationships are subtle and cover each other:
Intent先设置Data,后设置Type,那么Data被覆盖;Intent先设置Type,后设置Data,那么Type被覆盖;如果希望两者都存在,则应调用Intent的setDataAndType()方法。
The Data property receives a URI object, and a URI object usually represents the following form:
scheme://host:port/path// 例如:content://com.android.contacts/contacts/123
2) Next use the action, data property to start the system activity to browse the specified webpage, click the event as follows:
// 创建Intent=new Intent();Stringdata="http://www.126.com";// 根据指定字符串解析出Uri对象= Uri.parse(data);// 为Intent设置Action属性intent.setAction(Intent.ACTION_VIEW);// 设置Data属性intent.setData(uri);startActivity(intent);
This article summarizes the functionality and usage of intent in Android, and when an activity application needs to start a component, it always needs to be intent, as well as start service, Broadcastreceiver components are also implemented by intent.
Android Development Practice Intent