Android implicit intent configuration, android intent

Source: Internet
Author: User
Tags home screen

Android implicit intent configuration, android intent

URL: http://www.cnblogs.com/wuyudong/p/5677473.html.

The Android explicit activation of another Actitity article introduces a method for activating Activity.

This document uses the configuration file AndroidManifest. xml to configure the intent.

Description of Intent-filter attributes

If an Intent request executes an action on a piece of data, how does Android know which application (and component) can respond to this request?
Intent Filter is used to register Activity, Service, and Broadcast referers to execute an action on certain data.
Using Intent Filter, app components tell Android that they can provide services for the action requests of other program components, including the components of the same program, local or third-party applications.
To register an application component as an Intent processor, add an intent-filter tag to the component's manifest node.
Use the following tags (associated attributes) in the Intent Filter node to specify the actions, types, and data supported by the component:

1. Action Test

The <intent-filter> element can contain sub-elements <action>, for example:

<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 <intent-filter>;
(2) If the Intent request does not set the Action type, as long as <intent-filter> contains the Action type, this Intent request will pass the <intent-filter> behavior test smoothly.

2. Category Test

The <intent-filter> element can contain <category> sub-elements, such:

<intent-filter . . . >    <category android:name="android.Intent.Category.DEFAULT" />    <category android:name="android.Intent.Category.BROWSABLE" /></intent-filter >

The Intent request will pass 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.

3. Data Testing

The description of data in <intent-filter> is as follows:

<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 also specified in IntentFilter, they also need to match to pass the test.

Invalid action
Use the android: name feature to specify the action name for the response. Action names must be unique strings. Therefore, a good habit is to use a naming system based on Java packages.
Category

The Android: category attribute is used to specify the environment in which actions are responded. Each Intent Filter tag can contain multiple category tags. You can specify a custom type or use the standard value provided by Android as follows:

❑ ALTERNATIVE
As you will see later in this chapter, the purpose of an Intent Filter is to use actions to help fill in the context menu. The ALTERNATIVE type is specified. In a data type project, the default action can be replaced. For example, when a contact's default action is viewed, it may be edited or deleted.
❑ SELECTED_ALTERNATIVE
Similar to ALTERNATIVE, ALTERNATIVE always points to a single action using the Intent resolution described below. SELECTED_ALTERNATIVE is used when a list of possibilities is required.
❑ BROWSABLE
Specifies the action in the browser. When Intent is triggered in the browser, it is specified as a BROWSABLE type.
❑ DEFAULT
Set this type to make the component the default action of data defined in the Intent Filter. This is also necessary for activities started with explicit Intent.
❑ GADGET
By setting the GADGET type, you can specify that this Activity can be embedded into other activities to allow.
Administrative HOME
HOME Activity is the first Activity displayed when the device starts (login screen. By specifying the Intent Filter as the HOME type without specifying the action, you are setting it as an alternative to the local home screen.
❑ LAUNCHER
This type is used to make an Activity as the startup Item of the application.
❑ Data
The data tag allows you to specify the data matching that the component can act on. If your component can process multiple conditions, you can include multiple conditions. You can use any combination of the following attributes to specify the data supported by the component:
Using android: host
Specify a valid Host Name (for example, com. google ).
Using android: mimetype
You can set the data types that the component can process. For example, <type android: value = "vnd. android. cursor. dir/*"/> can match any Android cursor.
Using android: path
Valid URI path value (for example,/transport/boats /).
Using android: port
Valid port on a specific host.
Using android: scheme
A special illustration (such as content or http) is required ).

Practice

Add the Redirect function in Android explicit activation of another Actitity to implement implicit intent activation of the third interface.

Code:

// Use implicit Intent to activate public void click3 (View view) {intent Intent = new intent (); Intent. setAction ("com. wuyudong. xxx "); intent. addCategory ("android. intent. category. DEFAULT "); // intent. setData (Uri. parse ("wuyudong: hahaha"); // specify the Data Type // intent. setType ("vnd. android. cursor. item/xixixi "); intent. setDataAndType (Uri. parse ("wuyudong: hahaha"), "vnd. android. cursor. item/xixixi "); startActivity (intent );}

You can receive data in OtherScreenActivity. The Code is as follows:

Public class OtherScreenActivity extends Activity {// Override the onCreate method of the activity to set the initialization interface @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_two); Intent intent = getIntent (); // get the intent Uri to activate it = Intent. getData (); System. out. println (uri. getScheme ());}}

The code of the configuration file is as follows:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.wuyudong.twoactivity"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:icon="@drawable/icon1"            android:name="com.wuyudong.twoactivity.MainActivity"            android:label="@string/activity01" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>         <activity             android:icon="@drawable/icon2"            android:name="com.wuyudong.twoactivity.OtherScreenActivity"            android:label="@string/activity02" >            <intent-filter>                <action android:name="com.wuyudong.xxx"/>                <data android:scheme="wuyudong" android:mimeType="vnd.android.cursor.item/xixixi"></data>                <category android:name="android.intent.category.DEFAULT"/>            </intent-filter>        </activity>    </application></manifest>

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.