The Android programming interface is straightforward. As long as you understand activity and view, you can write out available applications. However, this is not enough. Android is a mobile phone operating system that provides various support related to networking and communication. With these support, you can develop programs that are close to the characteristics of mobile applications.
Intent is particularly worth noting in various programming concepts of Android. The English meaning of intent is "intention", and in the Android programming framework, it exactly reflects the concept of "Want to do something. It is used to transmit data between activities, or to request another activity to execute an operation by passing data. The following example shows the intent application.
Application Scenario: in a mobile phone Reader Program, the reader sees an article and feels good and wants to share it with friends.
Application display: press the menu key of the mobile phone to bring up the "share" menu. After clicking it, a series of sharing methods (SMS, email, and so on) are displayed ).
Background Knowledge: activity in different programs on Android can be called to each other. You can call the SMS program in your own program to send a text message. The method of calling is to make a request to the system, and the system will call the appropriate program activity.
The source code is as follows:
Package com. ghstudio. sharetest;
Import java. util. List;
Import Android. App. activity;
Import Android. content. intent;
Import Android. content. PM. packagemanager;
Import Android. content. PM. resolveinfo;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuitem;
Public class main extends activity {
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
/* Create a menu */
Public Boolean oncreateoptionsmenu (menu ){
Menu. Add (0, 0, 0, "share ");
Return true;
}
Public Boolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid ()){
Case 0:
Intent intent = new intent (intent. action_send );
Intent. settype ("text/plain ");
Intent. putextra (intent. extra_subject, "share ");
Intent. putextra (intent. extra_text, "I wowould like to share this with you ...");
Startactivity (intent. createchooser (intent, gettitle ()));
Return true;
}
Return false;
}
}
Code parsing: first, create an options menu with only one item "share ". When you click a menu item, create an intent. This intent is set to send to activity that supports action_send. Use two putextra commands to set the data of subject and text for intent, and then use the startactivity method to let the system call the appropriate activity for execution. The createchooser method accepts intent parameters and filters (action_send) required by intent. Only the activity that supports action_send is included in the optional list.
Run:
Figure 1 Press the menu key to display the share menu
Figure 2 shows the list of programs supporting action_send
Figure 3 click "information" to go To the SMS page