Intent Series Explanation---Intent Introduction and related properties

Source: Internet
Author: User

I. Intent INTRODUCTION

Intent Chinese is "intentions, intentions", it is the four components of Android communication link, intent is responsible for the application of the action, action involving data, additional data description, Android according to this intent description, responsible for finding the corresponding components, will Intent is passed to the calling component and completes the call to the component. Intent can be used not only between applications, but also between Activity/service within an application. As a result, intent can be understood as a "medium" for communication between different components, specifically providing information about what components call each other.

Two. How to start a component

Three. Properties of the intent (7)

  

Component (component): The purpose component----action: The act of expressing intent----Category: The category used to represent the action----data: The information that represents the action to manipulate

Type (datatype): Description of the data paradigm----extras (extended information): Extended information----flags (flag bit): The mode of operation that expects this intent

Three. Code implementation

(1) Componentactivity.class

  

/** * Direct Lookup method (via component name) */public class Componentactivity extends Appcompatactivity {@Override protected void onCreate (B        Undle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (r.layout.activity_component); }/** * Direct Lookup method (via component name) * @param view */public void Componetclick (view view) {/*intent Intent = n        EW Intent ();        ComponentName componentname = new ComponentName (this,intentactivity.class);        Intent.setcomponent (componentname); */Intent Intent = new Intent (This,intentactivity.class);    StartActivity (Intent); }/** * Use the indirect method (via the Action property and category property) * @param view * In the activity's manifest profile, you must use the default category * Android.intent.categ Ory.        DEFAULT */public void Actionclick (view view) {/*intent Intent = new Intent ();        Intent.setaction ("Com.langdon.action.MY_ACTION"); */Intent Intent = new Intent ("Com.langdon.action.MY_ACTION"); Intent.addcategory ("Com.langdon.category.My_category ");    StartActivity (Intent); }/** * Data property, typically used in conjunction with action (the most common way to use) * Type property, which represents the type of data * @param view */public void Dataclick (Vi        EW view) {Intent Intent = new Intent ();        Intent.setaction ("Com.langdon.action.MY_ACTION");        Intent.setaction (Intent.action_view);        Uri data = Uri.parse ("http://www.baidu.com"); Intent.setdata (data),//will set the Type property to null//intent.settype ("text/html"),//will set the Data property to NULL//To use type and data, you must use the        The following methods must match each other in order to pass Intent.setdataandtype (data, "text/html");    StartActivity (Intent);  The/** * Code implements the activity's startup mode * @param view */public void FlagClick (view view) {Intent Intent = new        Intent (This,flagactivity.class); Set the activity's startup mode//intent.flag_activity_new_task---Start the activity in a new task, and if so, start//intent.flag_activity_c in this task Lear_task---Equivalent to singletask//intent.flag_activity_clear_top---equivalent to singletop intent.setflags (InteNt.        Flag_activity_new_task);    StartActivity (Intent); }}

  

(2) Activity_component.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:id=" @+id/activity_componer "android:layout_width= "Match_parent" android:layout_height= "match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" Andro    id:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= " Com.langdon.taiyang.androidtest.Intent.ComponentActivity "> <button android:id=" @+id/bt_component "a Ndroid:text= "Find" by component Component name android:onclick= "Componetclick" android:layout_width= "Match_parent" an droid:layout_height= "Wrap_content"/> <button android:id= "@+id/bt_action" android:layout_below= "@+i    D/bt_component "android:text=" looks for "android:onclick=" through the action component name Actionclick "    Android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> <button android:id= "@+id/bt_data" android:layout_below= "@+id/bt_action" android:text= "Find" android:onclick= "data from the data component name Click "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "/> <button A Ndroid:id= "@+id/bt_flag" android:layout_below= "@+id/bt_data" android:text= "flag boot Mode" android:onclick= "FlagClick" android:layout_width= "match_parent" android:layout_height= "Wrap_content"/></relativelayou T>

  

(3) Androidmanifest.xml (intentactivity---nextactivity---dataactivity all contain only one textview, Used to display different text in Componentactivity.class by clicking the button)

<activity android:name= ". Intent.componentactivity "> <intent-filter> <action android:name=" Android.intent.acti On. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filte r> </activity> <activity android:name= ". Intent.intentactivity"> <intent-filter android:priority="-1 "> <action android:name=" Com.langdon.action.MY _action "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-fil ter> </activity> <activity android:name= ". Intent.nextactivity"> <intent-filter android:priority=" 2 "> <action android:name=" com.langdon.action.MY_  ACTION "/> <category android:name=" Android.intent.category.DEFAULT "/> <category Android:name= "Com.langdon.category.MY_CATEGORY"/> </intent-filter> </activity> & Lt;activity android:name= ". Intent.dataactivity"> <intent-filter> <action android:name=" Android.intent.action.VIEW "/> <category android:name= "Android.intent.category.DEFAULT"/> <data android:host= "www.baidu.co M "android:mimetype=" text/html "android:scheme=" http "/> </intent-filter> &l T;/activity> <activity android:name= ". Intent.flagactivity "/>

  

The results of the operation are as follows:

Figure 1

Figure 2

Figure 3

Where Figure 3 is the click of the third button to display, the remaining three button click will have a 1 textview

Shown

Intent Series Explanation---Intent Introduction and related properties

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.