Android intent stealth boot and intent Filter

Source: Internet
Author: User

Explicit intent Definition: Intent that explicitly specifies the name of the target component is called an explicit intent. Explicit intent uses the component name to define the target component directly. However, because developers often do not know the component names of other applications, explicit intent is more used to transmit messages within the application. For example, in an application, an activity starts a service.
Implicit intent Definition: Intent that does not explicitly specify the name of the target component is called implicit intent. It does not define the target component to be activated using the component name. It is more widely used to transmit messages between different applications. because there is no clear target component name, the android system must help the application find the component that best matches the intent request intent.

Enable intent implicitly:

The Android system looks for components that best match the intent request intent. The specific selection method is as follows:
Android compares the intent request content with a filter called intentfilter. intentfilter contains all possible components to be selected in the system. if a component in intentfilter matches the content of the implicit intent request, Android selects this component as the target component of the implicit intent.
How does Android know that an application can process some kind of intent requests? This requires the application to declare in the Android-Manifest.xml the filter of its own components (that is, the intent requests that can be matched ).
A component that does not declare intent-filter can only respond to explicit intent requests that specify its own name, but cannot respond to implicit intent requests.
A component that declares intentfilter can respond to both explicit intent requests and implicit intent requests. When parsing implicit intent requests by comparing with intentfilter, Android uses the following three factors as the reference standard action \ data \ category, extra and flag do not work when parsing and receiving intent

Intent filter:

 The application components can declare one or more intent filters to notify Android users of the implicit intent requests they can respond to and process. Each intent filter describes the type of request behavior and type of request data that the component can respond.
How to declare your intent filter for the component? A common method is to use the attribute <intent-filter> In the androidmanifest. xml file to describe the intent filter of the component.
When the implicit intent and intent filters are compared, the intent action, data, and category are the three elements. In fact, an implicit intent request must be passed to the target component through these three checks. Android will not pass the hidden intent to the target component if it does not match any aspect. Next we will explain the specific rules for these three checks.

Action Test: <Intent-filter> <action Android: Name = "COM. example. project. show_current "/> <action Android: Name =" COM. example. project. show_recent "/> <action Android: Name =" COM. example. project. show_pending "/> </intent-filter>
A <intent-filter> element must contain at least one <action>. Otherwise, no intent request can match the <intent-filter> element.
If the action requested by intent matches a <action> in <intent-filter>, the intent passes the <intent-filter> action test.
If the action type is not specified in the intent request or <intent-filter>, the following two cases may occur. (1) If <intent-filter> does not contain any action type, no intent request can match the <intent-filter> parameter. (2) otherwise, if the action type is not set in the intent request, the intent request will pass the <intent-filter> behavior test as long as <intent-filter> contains the action type.

CATEGORY Test: <Intent-filter> <category Android: Name = "android. intent. category. default "/> <category Android: Name =" android. intent. category. browsable "/> </intent-filter>
The intent request passes the test only when all the category in the intent request matches the <Category> of an intentfilter in the component.
The redundant <Category> declaration in intentfilter does not cause a matching failure.
An intentfilter that does not specify any category for testing only matches intent requests that do not set a category.

Data testing:

<Intent-filter> <data Android: TYPE = "Video/MPEG" Android: Scheme = "HTTP ".. /> <data Android: TYPE = "audio/MPEG" Android: Scheme = "HTTP ".. /> </intent-filter>
The <DATA> element specifies the data Uri and Data Type of the intent request to be accepted. The URI is divided into three parts for matching: Scheme, authority, and path. The URI data type and scheme of the inteat request set with setdata () must be consistent with the one specified in intentfilter. If authority or path is specified in intentfilter, they also need to match to pass the test.

This example uses intent to start the activity and display the values of action and category. Two la s are used in this example. Note that uses-permission is set in androidmainfest.

First, let's look at the layout file main. xml.

View code

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
3 Android: Orientation = "vertical"
4 Android: layout_width = "fill_parent"
5 Android: layout_height = "fill_parent"
6>
7 <button Android: Id = "@ + ID/Bt1"
8 Android: layout_width = "fill_parent"
9 Android: layout_height = "wrap_content"
10 Android: text = "intent stealth start test"
11/>
12 </linearlayout>

Second. xml file

View code

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:orientation="vertical"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent">
7 <EditText android:id = "@+id/et1"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"/>
10 <EditText android:id ="@+id/et2"
11 android:layout_width="fill_parent"
12 android:layout_height="wrap_content"/>
13 </LinearLayout>

Then let's take a look at the code in androidmainfest.

View code

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
3 package = "cn.shaoyangjiang.com"
4 Android: versioncode = "1"
5 Android: versionname = "1.0" type = "codeph" text = "/codeph">
6 <uses-SDK Android: minsdkversion = "8"/>
7
8 <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
9 <activity Android: Name = ". intentfilteractivity"
10 Android: Label = "@ string/app_name">
11 <intent-filter>
12 <action Android: Name = "android. Intent. Action. Main"/>
13 <category Android: Name = "android. Intent. Category. launcher"/>
14 </intent-filter>
15 </activity>
16
17 <activity Android: Name = ". Second"
18 Android: Label = "@ string/app_name">
19 <intent-filter>
20 <! -- Specify that the activity can respond to the action as the intent of the specified string -->
21 <action Android: Name = "cn.shaoyangjiang.cn.com"/>
22 <! -- Specifies that this activity can respond to the intent of the specified string as category -->
23 <category Android: Name = "cn.shaoyangjiang.cn"/>
24 <! -- Specify that the activity can respond to the Android. Intent. Category. Default intent -->
25 <category Android: Name = "android. Intent. Category. Default"/>
26 </intent-filter>
27 </activity>
28 </Application>
29 <uses-Permission Android: Name = "android. Permission. read_contacts"/>
30 <uses-Permission Android: Name = "android. Permission. write_contacts"/>
31 </manifest>

Step 3: Modify the intentfilteractivity. Java File

  

View code

1 package cn.shaoyangjiang.com;
2
3 Import Android. App. activity;
4 Import Android. content. intent;
5 import Android. OS. Bundle;
6 Import Android. View. view;
7 Import Android. View. View. onclicklistener;
8 Import Android. widget. Button;
9
10 public class intentfilteractivity extends activity {
11 // define an action constant
12 Final Static string Shao = "cn.shaoyangjiang.cn.com ";
13 // define a category constant
14 Final Static string Yang = "cn.shaoyangjiang.cn ";
15 @ override
16 public void oncreate (bundle savedinstancestate ){
17 super. oncreate (savedinstancestate );
18 setcontentview (R. layout. Main );
19 button button1 = (button) findviewbyid (R. Id. Bt1 );
20 button1.setonclicklistener (New onclicklistener (){
21 @ override
22 public void onclick (view arg0 ){
23 intent = new intent ();
24 // set Action attributes
25 intent. setaction (Shao );
26 // Add the category attribute
27 intent. addcategory (Yang );
28 startactivity (intent );
29}
30 });
31}
32}

The last part is the second. Java file.

View code

1 package cn.shaoyangjiang.com;
2
3 Import java. util. Set;
4 Import Android. App. activity;
5 import Android. OS. Bundle;
6 Import Android. widget. edittext;
7
8 public class second extends activity {
9 @ override
10 protected void oncreate (bundle savedinstancestate ){
11 super. oncreate (savedinstancestate );
12 setcontentview (R. layout. Second );
13 edittext edittext1 = (edittext) findviewbyid (R. Id. ET1 );
14 // obtain the intent action attribute corresponding to the activity
15 string actionvalue = getintent (). getaction ();
16 // Display Action attributes
17 edittext1.settext ("Action:" + actionvalue );
18 edittext edittext2 = (edittext) findviewbyid (R. Id. et2 );
19 // obtain the category attribute of the intent corresponding to the activity
20 set <string> Cate = getintent (). getcategories ();
21 // Display Action attributes
22 edittext2.settext ("categoties:" + Cate );
23}
24}

:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.