"Qianfeng-android-day06-intent" Seven major attributes of the basic learning:

Source: Internet
Author: User

intent and its seven properties and Intent-filter settings

first, the Knowledge Point review:Activity

(a), how to achieve Activity page jump?

Example code:

The first way:

Intent Intent = new Intent (mainactivity.this,nextactivity.class);

StartActivity (Intent);

The second way:

Intent Intent = new Intent ();

Intent.setclass (Mainactivity.this, Nextactivity.class);

StartActivity (Intent);

In fact, there are many kinds of intent to achieve page jump writing.

Second,Intent Object Description:(a),Intent Basic Introduction:

1. Intent is used to encapsulate the "invocation intent" of the program. Between two activity, the data that needs to be exchanged can be encapsulated into bundle object, then carry the bundle object with intent, and realize the exchange of data between two activity;

2. Intent is also an important medium for communication between various application components. Whether you want to start a acitivity, service, or broadcastreceiver,android, use a unified intent object to encapsulate this "start intent." It is obvious that using intent provides a consistent programming model;

3, intent also has a benefit, if the application just want to start a component with a certain feature, and do not want to be coupled with a specific component, you can configure the appropriate properties in the Intent-filter to handle, similar to the MVC framework in STUCTS2.

4, intent objects roughly include 7 major attributes: ComponentName, Action, Category, Data, Type, Extra, Flag.

(ii),intent how to start different components:

1. Start activity:

StartActivity ()

Startactivtyforresult ()

2. Start Service: "Detailed later"

Componetname StartService ()

Boolean Bindservice ()

3, start Broadcastreceiver: "Detailed later"

Sendbroadcast ()

Sendorderedbroadcast ()

Sendstickybroadcast ()

Sendstickyorderedbroadcast ()

Third,Seven properties of intent: "Important"

The intent object roughly includes 7 major properties: ComponentName, Action, Category, Data, Type, Extra, Flags.

Action as an identifier, represents a intent, when an activity requires external assistance to process, will emit a intent, if a program can complete the corresponding function, as long as the Intent-filter plus this intent.

Data is stored in a format that needs to be passed, such as: tel://

Extras Save the extra data that needs to be passed.

Category represents the type of intent, there are many ways to launch activity from Android, such as a list of programs, desktop icons, clicks on Home-activated desktops, etc. The category is used to identify the context in which the activity's icons appear in the startup contexts.

(a),componentname Properties:

1. The intent that specifies the ComponentName attribute has already clarified which component it will start, so this intent is called an explicit intent, and componentname with no specified intent attribute is called implicit intent. Implicit intent does not explicitly specify which component to start, and the application launches the eligible component according to the rules specified by intent.

2. Sample code:

Intent Intent = new Intent ();

ComponentName cName = new ComponentName (mainactivity.this,nextactivity.class);

Intent.setcomponent (CName);

StartActivity (Intent);

In fact, the above notation is simplified to the following:

Intent Intent = new Intent (mainactivity.this,nextactivity.class);

StartActivity (Intent);

That is to say, usually our most commonly used intent page jumps the wording to call is the explicit intent.

in addition, The ComponentName property enables one app to jump to another app.

Intent Intent = new Intent ();

ComponentName cName = new ComponentName (

"Com.steven.testasyncloader.sqlitedata", "com.steven.testasyncloader.sqlitedata.MainActivity");

Meaning of the two parameters: the first is the package name of the app to jump to, the second parameter is the class of the page in the package to jump to the app

Intent.setcomponent (CName);

StartActivity (Intent);

(ii),Action, Category property, and Intent-filter configuration:

Usually, The Action, category property is used in conjunction with the definition of both properties in the <intent-filter> node of the configuration file. Intent by defining the Action property (which is actually a custom string), you can isolate the intent from the specific activity to realize decoupling. Otherwise, each jump is written in a form like new Intent (Mainactivity.this,nextactivity.class), which means that the name of the target activity to be jumped is written out, so that the encoding is actually "hard coded", does not achieve loose coupling. using the Setaction () method of the intent object to implement a page jump is slightly more complex (it needs to be configured in the Androidmanifest.xml file), but the implementation is decoupled.

1. Sample code:

Intent Intent = new Intent ();

Intent.setaction ("com.steven.android06lifecycle.nextactivity");

StartActivity (Intent);

When registering an activity in a configuration file, you need to declare:

<activity android:name= "Com.steven.android06lifecycle.NextActivity" >

<intent-filter>

<action android:name= "Com.steven.android06lifecycle.nextactivity"/>

<category android:name= "Android.intent.category.DEFAULT"/>

</intent-filter>

</activity>

When a page is the default startup page, you need to define the action, category property must be the following string: "Set Task Entry"

<intent-filter>

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

<category android:name= "Android.intent.category.LAUNCHER"/>

</intent-filter>

2. Common Action Property constants:

The intent object not only launches the program components within the application, but also launches the components of other apps for the Android system, including the system's built-in program components (permissions are required).

Action_main: (Android.intent.action.MAIN) Android program entry.

each Android apps must and can contain only one action declaration of this type. "If more than one is set, which is before and which is executed." 】

Action_view: (Android.intent.action.VIEW) Displays the specified data.

Action_edit: (Android.intent.action.EDIT) edits the specified data.

Action_dial: (Android.intent.action.DIAL) displays the Dial panel.

Action_call: (Android.intent.action.CALL) Call the number directly in data.

Action_answer: (Android.intent.action.ANSWER) answer the call.

Action_send: (Android.intent.action.SEND) Send data to other people (for example, MMS/email).

Action_sendto: (android.intent.action.SENDTO) Send text messages to other people.

Action_search: (Android.intent.action.SEARCH) performs the search.

Action_get_content: (Android.intent.action.GET_CONTENT) lets the user select the data and return the selected data.

(iii),Category attribute:

The Category property adds additional additional category information to the action. Category_launcher means that acticity appears at the top when the program is loaded, and category_home means that the page jumps to the home screen.

1, the implementation of the page to jump to the home interface code: "Memory"

Intent Intent = new Intent ();

Intent.setaction (Intent.action_main);

Intent.addcategory (Intent.categoty_home);

StartActivity (Intent);

2. Common Category Attribute constants:

Category_default: (Android.intent.category.DEFAULT) The default mode of execution in an Android system is performed according to normal activity execution.

Category_home: (Android.intent.category.HOME) Set the component as the HOME Activity.

Category_launcher: (Android.intent.category.LAUNCHER) sets the component to be the highest priority activity in the current application launcher, typically used in conjunction with the Ingress Action_main.

Category_browsable: (Android.intent.category.BROWSABLE) Setting the component can be started using the browser.

(iv),Data property:

1. The data property is typically used to provide the Action property with the operation's information. The value of the Data property is a URI object.

The format of the URI is as follows: Scheme://host:port/path

2, the system built-in several data property constants:

TEL://: The number data format followed by the phone number.

MAILTO://: The message data format, followed by the mail recipient address.

SMSTO://: Short data format followed by SMS receiving number.

CONTENT://: The content data format, followed by what needs to be read.

file://: File data format followed by file path.

Market://search?q=pname:pkgname: Market data format, search for apps with the package name Pkgname in Google markets.

Geo://latitude, Longitude: The latitude and Longitude data format that displays the location specified by latitude and longitude on the map.

3. Intent use the action attribute and the Data property to launch the code for the built-in components of the Android system: "No need to remember, use to find information"

(a), call:

Intent intent=new Intent ();

Intent.setaction (Intent.action_call);

Intent.setaction ("Android.intent.action.CALL"); There are two ways to do this.

Intent.setdata (Uri.parse ("tel:1320010001"));

StartActivity (Intent);

Call Dial Panel:

Intent intent=new Intent ();

Intent.setaction (intent.action_dial);

Intent.setdata (Uri.parse ("tel:1320010001"));

StartActivity (Intent);

Call Dial Panel:

Intent intent=new Intent ();

Intent.setaction (Intent.action_view);

Intent.setdata (Uri.parse ("tel:1320010001"));

StartActivity (Intent);

(ii), use URI Open Browser, open map, etc.:

Uri uri = uri.parse ("http://www.google.com"); Browser

Uri uri=uri.parse ("geo:39.899533,116.036476"); Turn on map positioning

Intent Intent = new Intent ();

Intent.setaction (Intent.action_view);

Intent.setdata (URI);

StartActivity (Intent);

(v),Type property:

1. The Type property is used to specify the MIME type for the URI specified by data. MIME as long as it conforms to the string format "abc/xyz".

2. Intent use the action, data, and type properties to start the code for the built-in components of the Android system:

(iii), play Video:

Intent Intent = new Intent ();

Uri uri = uri.parse ("File:///sdcard/media.mp4");

Intent.setaction (Intent.action_view);

Intent.setdataandtype (URI, "video/*");

StartActivity (Intent);

(vi),Extra Properties:

1, through the form of Intent.putextra (key, value) to exchange data between multiple activity.

2, the system built-in several extra constants:

EXTRA_BCC: An array of strings that hold the message's secret address.

EXTRA_CC: An array of strings that hold the e-mail cc addresses.

Extra_email: An array of strings that hold the e-mail address.

Extra_subject: Holds the message subject string.

Extra_text: Store the contents of the message.

Extra_key_event: The button that triggers intent is stored as a KeyEvent object.

Extra_phone_number: The phone number when the call to Action_call is stored.

3. Intent use the action, data, and type, extra properties to start the code for the built-in components of the Android system:

(iv), call the program to send SMS

Intent Intent = new Intent ();

Intent.setaction (Intent.action_view);

Intent.settype ("vnd.android-dir/mms-sms");

Intent.putextra ("Sms_body", "Information content ...");

StartActivity (Intent);

Send SMS

Uri uri = uri.parse ("smsto:13200100001");

Intent Intent = new Intent ();

Intent.setaction (Intent. ACTION_SENDTO);

Intent.setdata (URI);

Intent.putextra ("Sms_body", "Information content ...");

StartActivity (Intent);

(v), send Email:

Intent intent=new Intent ();

Intent.setaction (Intent. Action_send);

String[] tos={"[Email protected]"};

String[] ccs={"[Email protected]"};

Intent.putextra (Intent.extra_email, TOS);

Intent.putextra (INTENT.EXTRA_CC, CCS);

Intent.putextra (Intent.extra_text, "The email body TEXT");

Intent.putextra (Intent.extra_subject, "The email SUBJECT text");

Intent.settype ("message/rfc822");

StartActivity (Intent.createchooser (Intent, "Choose Email Client"));

Intent Intent = new Intent (intent.action_send);

String[] tos = {"[Email protected]"};

Intent.putextra (Intent.extra_email, TOS);

Intent.putextra (Intent.extra_text, Getphoneparameter ());

Intent.putextra (Intent.extra_subject, "Android Log");

Intent.putextra (Intent.extra_stream, Uri.fromfile (Cachedir));

Intent.settype ("message/rfc882");

Intent.settype ("Plain/text");

Intent.createchooser (Intent, "Please select Mail sending software");

StartActivity (Intent);

Intent.setaction (Android.provider.Settings.ACTION_SETTINGS);

4. Intent uses the Action_get_content in the Action property to get the return value:

Select the identity returned by the picture Requestcode

Intent Intent = new Intent ();

Intent.setaction (Intent. Action_get_content);

Intent.settype ("image/*");

Intent wrapperintent = Intent.createchooser (Intent, NULL);

Startactivityforresult (Wrapperintent, Requestcode);

Add Audio

Intent Intent = new Intent ();

Intent.setaction (Intent. Action_get_content);

Intent.settype ("video/*");

Intent wrapperintent = Intent.createchooser (Intent, NULL);

Startactivityforresult (Wrapperintent, Requestcode);

Video

Intent Intent = new Intent ();

Intent.setaction (Intent. Action_get_content);

Intent.settype ("video/*");

Intent wrapperintent = Intent.createchooser (Intent, NULL);

Startactivityforresult (Wrapperintent, Requestcode);

Recording

Intent Intent = new Intent ();

Intent.setaction (Intent. Action_get_content);

Intent.settype ("Audio/amr");

Intent.setclassname ("Com.android.soundrecorder", "Com.android.soundrecorder.SoundRecorder");

Startactivityforresult (Intent, Requestcode);

(vii),Flags Property: Intent can call the Addflags () method to add a control token to intent. "Important"

1, Flag_activity_clear_top: (Effect with ACTIVITY Launchmode singletask)

If you already have that in the stack Instance of the activity, the instance is reused. When reused, the instance is returned to the top of the stack, so the instances above it will be removed from the stack. If the instance does not exist in the stack, a new instance is created to be put into the stack.

2, Flag_activity_single_top: (Effect with ACTIVITY Launchmode singletop)

if the stack at the top of the task exists Activity, the instance is reused and no new activity object is created.

3, Flag_activity_new_task: (Effect similar to ACTIVITY Launchmode singleinstance)

Example code:

Intent Intent = new Intent (this, mainactivity.class);

The activity on the activity stack that is on top of the mainactivity Main page pops up.

Intent.setflags (Intent.flag_activity_clear_top);

StartActivity (Intent);

"Note:" The emphasis needs to be carefully understood.

For example:

If you start the four Activity:a, B, C, D.

in the D activity, jump to B activity, and hope D and C both finish off, you can add the flags tag in startactivity (intent) intent, as follows:

Intent Intent = new Intent (this, b.class);

Intent.setflags (Intent.flag_activity_clear_top);

StartActivity (Intent);

this starts B activity at the same time, will be D, C are finished off.

if  B Activity's Launchmode is the default "standard", then B activity will first finished off the old B page, and then start a new activity B. If you do not want to re-create a new B activity, but instead reuse the previous B activity, you can set the Launchmode of activity B to "Singletask". "In particular, it is important to note that in some phones, such as Samsung phones. Even Singletask will produce new pages instead of reusing previous pages. 】

"Qianfeng-android-day06-intent" Seven major attributes of the basic learning:

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.