Intent is an important way to interact between components in an Android program, and it can indicate not only the current group
The actions you want to perform, you can also pass data between different components. Intent can generally be used to initiate activities, start
Services, and sending broadcast scenarios
Intent is a runtime binding mechanism that connects two different components during a program's run. With the intent program you can express some kind of request or intention to Android, and Android will select the appropriate component to request according to the desired content.
In the communication between these components, mainly by the intent assistance to complete. Intent is responsible for the action of an operation in the application, the action involves data, the additional data is described, Android according to this intent description, is responsible to find the corresponding component, the intent passed to the calling component, and complete the call of the component.
As a result, intent plays a role as a media intermediary, specifically providing self-test-related information to each other, enabling decoupling between the caller and the callee.
For example: In a contact maintenance app, when we click on a contact in a contact list screen, we want to be able to jump out of this contact's details screen.
To achieve this, listactivity needs to construct a Intent, which is used to tell the system that we want to do a view of the action, this action corresponds to the View object is "a contact", and then call StartActivity (Intent Intent), By passing the constructed intent, the system will find the activity that satisfies this intent requirement according to the description in this intent, and the system will invoke the found activity, that is detailactivity, finally the intent, Detailactivity will perform the corresponding operation according to the description in this intent.
First, the layout interface, where there is only one button, when the button is clicked, it jumps to the other interface to display the contact details.
Create mainactivityFirst:
Package Com.example.intentdemo;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import android.widget.button;/** * Pass data through intent * For example: Phone contact, when a person in the point of time, will show this person's details * Of course it's done through another interface. * @author Caesar * */public class Mainactivity extends Activity implements Onclicklistener{private Button b Utton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (this );} @Overridepublic void OnClick (View v) {//TODO auto-generated method stub//Create a intent, Represents a jump from mainactivity to otheractivity activity//The first parameter represents the context of the start activity, the second parameter represents the target activity to be jumped, receives the. Class type Intent Intent = new Intent ( Mainactivity.this, Otheractivity.class);//For intent incoming data Intent.putextra ("name", "Zhang San"); Intent.putextra ("Age", 15); Intent.putextra ("Address", "Beijing"); INTENT.PUtextra ("tel", "121212121212");//Start Activity startactivity (Intent);}}
Because the program to jump to otheractivity, so should be in the creation of a otheractivity class, let it inherit activity, the OnCreate method of replication, and load other layout, and now there is no other layout, need to first to create.
Because otheractivity is primarily used to display information, only one textview in the other layout is used to display information.
Other.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <textview android:id= "@+id/msg" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"/></linearlayout>
Otheractivity.java
Package Com.example.intentdemo;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.widget.textview;public class Otheractivity extends Activity {private TextView textview;@ overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.other); TextView = (TextView) Findviewbyid (r.id.msg);//Get Intent Intent Intent = getintent ();//Get information in intent int age = Intent.getintextra ("Age", 0); String name = Intent.getstringextra ("name"); String address = Intent.getstringextra ("Address"); String Tel = Intent.getstringextra ("tel"); String Info = "name-->" + name + "\ n" + "age-->" + Age + "\ n" + "address-->" + address + "\ n" + "TEL:" + tel;//show the Information Textview.settext (info);}}
We need to add permissions to the otheractivity in the registry:
In application , the first activity is added below:<activity android:name=". Otheractivity " >
OK, run it:
OK, success.
Android---8---intent and pass data with intent