The communication between components in an Android application is intent, and intent is responsible for describing the action of one operation in the application, the action involved in the data, the additional data, and the android is responsible for finding the corresponding component according to the intent description, which will Intent is passed to the calling component and completes the call to the component. Intent is the meaning of intention. Intent is divided into two types: explicit (Explicit intent) and implicit (implicit intent).
Show Call Intent
A simple demo goes from one activity to another aactivity:
Mainactivity's Layout file
<edittext android:id= "@+id/edt_content" android:layout_width= "match_parent" android:layout_ height= "Wrap_content"/> <button android:id= "@+id/btn_login" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:layout_below=" @id/edt_content " android:o nclick= "Login" android:text= "Query"/>
To invoke a click event in mainactivity:
EditText contentedittext= (EditText) Findviewbyid (r.id.edt_content); Intent intent=new Intent (this,personactivity.class); Intent.putextra (Extra,contentedittext.gettext (). toString ()); StartActivity (Intent);
This time the intent is the display call, directly specify the receiving parameters of the activity, you can uniquely determine an activity, the intention is particularly clear, this time need to receive parameters in Personactivity:
public class Personactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_person); TextView textview= (TextView) Findviewbyid (r.id.txt_content); Intent intent=getintent (); String Str=intent.getstringextra (Mainactivity.extra); Textview.settext (str); textview.settextsize (20); Textview.settextcolor (color.red);}
Another way to pass parameters at this time is to use Putextra, and if you pass more arguments than you can use bundles similar to map.
Implicit invocation
Implicitly, instead of explicitly specifying the activity that needs to be invoked, implicitly specifying which activity to start, instead setting the action, Data, Category, and allowing the system to filter out the appropriate activity. The filter is filtered according to all <intent-filter>.
This time need to set up in the Androidmanifest.xml Intent-filter to set up, as below, the category directly using the default line:
<intent-filter> <action android:name= "Com.example.googleone.Peson"/> <category android: Name= "Android.intent.category.DEFAULT"/> </intent-filter>
The call in Mainactivity is used, this time the call:
Intent intent=new Intent ("Com.example.googleone.Peson"); StartActivity (Intent);
This self-defined action string can invoke the activity of its own program, as well as the action of other applications, such as a commonly used dial-up panel:
Intent Intent = new Intent (intent.action_dial); StartActivity (Intent);
If this time in the Androidmanifest.xml file to Personactivity, add an action, as follows:
<activity android:name= ". Personactivity " android:label=" @string/title_activity_person "> <intent-filter> <action Android:name= "Android.intent.action.DIAL"/> <action android:name= "Com.example.googleone.Peson"/> <category android:name= "Android.intent.category.DEFAULT"/> </intent-filter> </ Activity>
Calls in Mainactivity:
Intent intent=new Intent (intent.action_dial); if (Intent.resolveactivity (Getpackagemanager ()) = = null) { view.setenabled (false); } try {startactivity (intent);} catch (Activitynotfoundexception e) {//TODO auto-generated catch Blocktoast.maketext (this , "No corresponding activity found", Toast.length_short). Show ();
Results:
Intent.action_dial is a system constant string, equivalent to Android.intent.action.DIAL, when called by the name of the action, to find the activity~ with this action
Display and implicit use of intent in Android