Android Basics Learn the use of the third chapter-intent

Source: Internet
Author: User

1. Recently in self-study Android, is also reading while writing some demo, because of more and more knowledge points, the brain is more and more memory is not clear, so intends to write reading notes, for later review, but also to learn what they understand to write out, caught dead, if there is a wrong place, I hope everyone give and correct.

2. Because similar to reading notes, may format God horse will compare with (hen) meaning (chou), we can't stand, you can hard spit groove.

I'm just dividing the line ***************************************

The previous article has already told some things about activity, this time recording a more important class intent Android.

1. What is intent?

Intent is an important way of interacting between components in an Android program, which can typically be used to launch events, start services, and send broadcasts, which mainly describe the use of intent during the activation process. Its usage is broadly divided into two cases: explicit intent and implicit intent.

2. Show Intent Usage

Just like the previous story about activity initiation, the transfer of activity between activities is actually the use of explicit intent,

<2.1> page Normal Jump

New Intent (); _intent.setclass (mainactivity. this, testactivity. class ); startactivity (_intent);

The above means jumping from the active mainactivity to the active testactivity, without passing any data.

<2.2> page jump, and return value

In the mainactivity activity, click on the button to add the following code:

New Intent (); _intent.setclass (mainactivity. this, activityc. class  100);

In the ACTIVITYC activity, pass the data back:

    classButtonactivityalistenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {if(V.getid () = =r.id.btnreturn) {Intent _intent=NewIntent (); EditText EditText=(EditText) Findviewbyid (R.id.edittext_first); String Inputstr=Edittext.gettext (). toString (); _intent.putextra ("Dashenzaijia", INPUTSTR);                Setresult (ACTIVITY.RESULT_OK, _intent);            Finish (); }        }    }
There may be another situation, if the user is not the click button to return the previous activity, but the click of the phone's back button, this time just rewrite the onbackpressed method is good , as follows
@Override
public void onbackpressed () {
Intent _intent = new Intent ();
_intent.putextra ("Dashenzaijia", "need transfer value");
Setresult (ACTIVITY.RESULT_OK, _intent);
Finish ();
}

In the Onactivityresult method of the mainactivity activity, get the corresponding return value:

@Overrideprotected voidOnactivityresult (intARG0,intarg1, Intent arg2) {        //TODO auto-generated Method Stub        Super. Onactivityresult (arg0, arg1, arg2); if(arg0 = = && Arg1 = = ACTIVITY.RESULT_OK) {String returnvalue= Arg2.getstringextra ("Dashenzaijia"); TextView View= (TextView) This. Findviewbyid (R.id.textview_showresult); if(!Returnvalue.isempty ()) {View.settext ("Value from activity C:" +returnvalue); } Else{View.settext (NULL); }        }    }

Please note that the parameters in 100, there are ACTIVITY.RESULT_OK, they are two of the page value of the signal, only the password, to get the corresponding activity returned value.

<2.3> pass values to the next activity

In the mainactivity active Jump button, implement the following code:

    New Intent ();    _intent.setclass (mainactivity. this, testactivity. class );    _intent.putextra ("Transfertockey", "give current value to activity C");    StartActivity (_intent);

The OnCreate (Bundle savedinstancestate) method in the testactivity activity gets:

   Intent Intent = getintent ();    = Intent.getstringextra ("Transfertockey");   LOG.I ("OnCreate", value);

3. Implicit Intent Usage

<3.1.1> shows a new activity: As above 2.1 says, when we show a new activity, we can pass the class name of the new activity, or we can use the following method

In the Androidmanifest.xml file, add a configuration for the canvasactivity activity, customize an action, and add the default category:

        <ActivityAndroid:name=". Canvasactivity ">            <Intent-filter>                <ActionAndroid:name= "firstapp.canvasactivity.firstaction" />                <categoryAndroid:name= "Android.intent.category.DEFAULT" />            </Intent-filter>                </Activity>

And then mainactivity the activity, call the following method:

    class Implements Onclicklistener {        @Override        publicvoid  OnClick (View v) {            if (V.getid () = = r.id.btnshownotification) {                new Intent ("  Firstapp.canvasactivity.firstaction");                StartActivity (intent);     }}}

Note: <category android:name= "Android.intent.category.DEFAULT"/> This sentence must not be less, or will error.

<3.1.2> show a new activity, on the basis of 3.1.1, we can also customize a category,

        <ActivityAndroid:name=". Canvasactivity ">            <Intent-filter>                <ActionAndroid:name= "Firstapp.canvasactivity.firstaction" />                <categoryAndroid:name= "Android.intent.category.DEFAULT" />                <category  android:name= "Firstapp.canvasactivity.firstcategory" / >             </Intent-filter>        </Activity>

Call and 3.1.1 Almost, just one more sentence:

    classNotificationclicklistenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {if(V.getid () = =r.id.btnshownotification) {Intent Intent=NewIntent ("firstapp.canvasactivity.firstaction"); intent.addcategory ( "Firstapp.canvasactivity.firstcategory");            StartActivity (Intent); }        }    }    

<3.2.1> display a webpage, in the ACTIVITYB activity, click the button code, you can open the browser:

The configuration of activityb in the Androidmanifest.xml file is:

        <activity            android:name= ". Activityb "            android:label=" @string/title_activity_b ">        </activity>

The call is as follows:

        Button Btnopenweb = (Button) Findviewbyid (r.id.btnopenweb);        Btnopenweb.setonclicklistener (new  Onclicklistener () {            @Override            public void OnClick (View v) {                new  Intent (intent.action_view);                Intent.setdata (Uri.parse ("http://www.baidu.com"));                StartActivity (intent);            }        });

Note: Must be added in front of the URL http://, or will error, the system does not know what www.baidu.com is, should be opened with what.

<3.2.2> show a webpage, the Web page opens the same code as above, the only difference is that in androidmanifest.xml, we add a few lines of code to the configuration of the Activityb activity:

        <activity            android:name= ". Activityb "            android:label=" @string/title_activity_b ">            <intent-filter>                <action Android:name= "Android.intent.action.VIEW"/>                <category android:name= "Android.intent.category.DEFAULT"/ >                <data android:scheme= "http"/>            </intent-filter>        </activity>

Running the code, you will find that our activityb has also become an option to open the Web application.

<3.3> call map, show location

        Button Btnopenweb = (Button) Findviewbyid (r.id.btnopenweb);        Btnopenweb.setonclicklistener (new  Onclicklistener () {            @Override            public void OnClick (View v) {                new  Intent (intent.action_view);                Intent.setdata (Uri.parse ("geo:114.06667,22.61667?q= Hi-tech Park MRT Station");            }        );

As with 3.2.2, if a <data android:scheme= "Geo"/> is added to the activityb of Androidmanifest.xml; Activityb also becomes one of the options to open the list of locations.

<3.4> Call system dialing

        Button Btnopenweb = (Button) Findviewbyid (r.id.btnopenweb);        Btnopenweb.setonclicklistener (new  Onclicklistener () {            @Override            public void OnClick (View v) {                new  Intent (intent.action_view);                Intent.setdata (Uri.parse ("tel:15811588511"));                StartActivity (intent);            }        });

Well, the basic usage of intent is to say here, what's the problem, please correct me.

Android Basics Learn the use of the third chapter-intent

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.