Intent can be said to be the soul of Android, the program jumps and transmits data is basically by intent. Intent is very important in Android applications, and it is very helpful to understand that the intent is programmed accordingly. In the official Android API document, the Intent is defined as: An Intent was an abstract description of a operation to be performed. A intent is an abstract descriptive narrative of the operation to be run (real enough abstract).
There are 3 types of details:
1, through the StartActivity method to start a new activity.
2, through the broadcast mechanism can send a intent to whatever is interested in this intent broadcastreceiver.
3, through StartService (Intent) or Bindservice (Intent, serviceconnection, int) to interact with the service behind the scenes.
To see how to start a new activity through the StartActivity method. The most common use of the
intent is to connect an application's various activity, assuming we compare activity to a building block. Then intent is like glue. Glue the different bricks together to form the house we built. In a program where it is assumed to start an activity, we usually call the StartActivity method and pass the intent as a reference. For example, see the following:
startactivity (intent);
This intent either specifies an activity, or contains only information about the selected activity. However, it is up to the system to decide which activity to start in detail, and the Android system is responsible for selecting the activity that best meets the matching selection criteria.
Instance: Intentdemo
Execution Effect:
Code Listing:
Androidmanifest.xml
<?XML version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" package= "Com.rainsong.intentdemo" android:versioncode= "1" android:versionname= "1.0" > < USES-SDK android:minsdkversion= "one" android:targetsdkversion= "/> <application android: Label= "@string/app_name" android:icon= "@drawable/ic_launcher" > <activity android:name= "Mainactivity" android:label= "@string/app_name" > <intent-filter> <action android:name= " Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <activity android:name= "otheractivity" Android:label = "Otheractivity" > </activity> </application></manifest>
Layout file: Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" match_parent " android:layout_height= "Match_parent" > <button android:id= "@+id/button1" android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "jump to another activity" /></linearlayout>
Layout file: Other.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" match_parent " android:layout_ height= "Match_parent" ><textview android:layout_width= "match_parent" android:layout_ height= "Wrap_content" android:text= "otheractivity" /></linearlayout>
Java source file: Mainactivity.java
Package Com.rainsong.intentdemo;import Android.app.activity;import Android.content.intent;import android.os.Bundle ; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends activity{ onclicklistener listener1 = null; Button button1; /** called when the activity is first created. */ @Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate) ; Setcontentview (r.layout.main); Listener1 = new Onclicklistener () {public void OnClick (View v) { Intent intent1= New Intent ( Mainactivity.this, otheractivity.class); StartActivity (INTENT1); } }; Button1 = (Button) Findviewbyid (r.id.button1); Button1.setonclicklistener (Listener1); }}
Java source file: Otheractivity.java
Package Com.rainsong.intentdemo;import Android.app.activity;import Android.os.bundle;public class OtherActivity Extends activity{ /** called when the Activity is first created. * /@Override public void OnCreate (Bundle save Dinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.other); }}
Android's intent and activity