7th lesson for video tutorials for Joshui teachers
This course explains intent, an abstract description of the action, meaning of intent, and the creation of a intent can give an action to him to execute. Inten is the essence of Android development.
One, click the button to make a call to a specific number
Btn1.setonclicklistener (listener); Private Onclicklistener Listener =new Onclicklistener () {Intent Intent = new Intent (); @Overridepublic void OnClick (View v) {intent.setaction (intent.action_call); Intent.setdata (Uri.parse ("tel:110") to give intent the action of calling; Set phone number startactivity (intent); Start Toast.maketext (mainactivity.this, "call", Toast.length_short). Show ();//Display Prompt}};
Two, click the button to send a text message to a specified number
Btn1.setonclicklistener (listener); Private Onclicklistener Listener =new Onclicklistener () {Intent Intent = new Intent (); @Overridepublic void OnClick (View v) {Case r.id.button1:intent.setaction (intent.action_sendto); Intent.setdata (Uri.parse ("smsto:110")); Intent.putextra ("Sms_body", "nothing, harassment under ~ ~"); Note the format startactivity (intent); Toast.maketext (Mainactivity.this, "Send Text to", Toast.length_short). Show ();//Display Prompt}};
Three, start another activity in one activity
The most basic set of activity to include:
1,activity Class (Xxactivity.java)
2, Layout file (layout/xxx.xml)
3, register the activity in the configuration file
<activity
Android:name= "xxxxx"
Android:label= "XXXX" >
..................
</activity>
With these basic elements, you can invoke the activity in another activity.
For convenience, put this jump in a button click event. Intent intent=new Intent (); Intent.setclass (mainactivity.this,secondactivity.class);//Parameter 1 is Packagecontext, Usually point to mainactivity himself; parameter 2 is the classstartactivity (intent) where the CLS points to the activity to invoke;
Configuration file Join:
<activity android:name= ". Secondactivity " //Same as activity name android:label=" @string/app_name "> //Modify </activity> as required
These are the most basic content, if you have a function in the secondactivity, the runtime can be implemented from the mainactivity jump to secondactivity to perform its functions. How do you pass data between the activity?
Sender:
Intent intent=new Intent (); Intent.setclass (Mainactivity.this,secondactivity.class); Intent.putextra ("str", "Hello" ); PutExtra (); Can send many types of data!startactivity (intent);
Receiving Party:
Intent intent=getintent (); Bundle Bundle=intent.getextras (); What is a bundle? Add to the commentary. String str=bundle.getstring ("str");//characters are stored in Str.
Then it is interesting, it can be understood that: the realization of clicking a button to jump to another interface, then you have to click on a button in the other interface to jump back, and also to return a data.
Defined in Mainactivity:
Private final static int request_code=1; Equivalent to a special code, for the following definition of intent all, if more than one definition of intent will be assigned a different password, why assign a password? Just to one-to-many situations
The button listens to write:
Intent intent=new Intent (); Intent.setclass (Mainactivity.this,secondactivity.class); Intent.putextra ("str", "Hello" );//startactivity (intent); Startactivityforresult (Intent,request_code) ; StartActivity (intent); There is no return value, this one. But to write a //onactivityresult method is as follows
protected void Onactivityresult (int requestcode,int resultcode,intent data)//Learn to construct it with a method constructor! {if (Requestcode==request_code)//Prevent many-to-one {if (Resultcode==secondactivity.result_code)//prevent a-to-many {Bundle bundle= Data.getextras (); String str=bundle.getstring ("Back"); Toast.maketext (Mainactivity.this,str,toast.length_long). Show ();}}}
Defined in Secondactivity:
Public final static int result_code=1;
The button listens to write:
Intent intent=new intemt () Intent.putextra ("Back", "back Data"); Setresult (result_code,intent); Set the return value of finish (); Kill the current Activity
That's ok! Think about it, is Android software easy?
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Learning Notes (4) Intent Primary learning