Initial address
Http://www.eoeandroid.com/thread-228079-1-1.html
1 case objective
In this case, a simple communication tool is used to consolidate the layout of Android activity, linearlayout, basic control button, call and send text messages, and applications.ProgramUse of interactive communication intent. This case focuses on the event processing principles in Android and Its Application in projects.
2. Case Study This communication tool allows you to send text messages and make phone calls. No matter in the ends of the earth, you only need to use the tool, so that you can communicate with your friends and family in a distance, without any access, let your life go from scratch.
3 Case Analysis This project is developed using android2.3.3. Because this project mainly implements the call and text messaging function, use intent to activate the program-call and the intent call program used in this text message, the intent action is action_dial, at the same time, the phone number of the caller is transmitted in intent. You must add uses-Permission to androidmanifest and declare the Android: Name = "android. Permission. call_phone" permission. The reason is that the phone call is the underlying service of the mobile phone, and messages are sent to the user's privacy and call charges.The program must have permissions. Secondly, the "action_call" Key (Action) is introduced through the custom intent object and the URI. the parse () method is used to bring the user into the phone number (data), and The startactivity () method is used to complete the process.To enable the SMS function, you must use the permission to send text messages in Android, that is, add the following content to andoridmanifest. xml:
<Uses-permissionandroid: Name = "android. Permission. send_sms"/>
4 case design
When you click "text message" and "call", the event processing process is as follows: (1) Create an event source for "text message" and "call" buttons. (2) The event source registers with the click event listener class. (3) When you click "send text message" and "call", the event is triggered and The onclick (View) method in the event listener class is called to process the business logic.
5. Business Logic Design
If(Myedittext. gettext (). Length ()> 0{Intent myintent=NewIntent (intent. action_call, Uri. parse ("Tel:" +Myedittext. gettext (). tostring (); phone.This. Startactivity (myintent);} string mobile=Myedittext. gettext (). tostring (); string content=Edittext2.gettext (). tostring (); smsmanager SMS=Smsmanager. getdefault (); pendingintent sentintent= Pendingintent. getbroadcast (smsactivity.This, 0,NewIntent (), 0 );
First come:
6 case implementation:
Important part of case implementation Code As follows: 1. Home Page
<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" Android: Background = "@ Drawable/BG" Android: Orientation = "Vertical"> < Button Android: ID = "@ + ID/button1"Android: layout_width = "Wrap_content" Android: layout_height = "Wrap_content" Android: layout_alignparentbottom = "True" Android: layout_alignparentleft = "True" Android: layout_marginbottom = "165dp" Android: layout_marginleft = "23dp" Android: Background = "@ Drawable/button" Android: Text = "Call"/> < Button Android: ID = "@ + ID/button2" Android: layout_width = "Wrap_content" Android: layout_height = "Wrap_content" Android: layout_alignbaseline = "@ + ID/button1" Android: layout_alignbottom = "@ + ID/button1" Android: layout_alignparentright = "True" Android: Background = "@ Drawable/button" Android: Text = "Text message"
2. Main Interface Activity
Public Class Smsandphoneactivity Extends Activity { /** Called when the activity is first created. */ Button button1, button2; @ override Public Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main ); // Search for corresponding controls by ID Button1 = (Button) findviewbyid (R. Id. button1); button2 = (Button) findviewbyid (R. Id. button2 ); // Button listening event Button1.setonclicklistener ( New Onclicklistener () {@ override Public Void Onclick (view v ){ // Todo auto-generated method stub // Go to the call page Intent intent = New Intent (smsandphoneactivity. This , Phone. Class ); Startactivity (intent) ;}}); button2.setonclicklistener ( New Onclicklistener () {@ override Public Void Onclick (view v ){ // Todo auto-generated method stub // Jump to the text message page Intent intent1 = New Intent (smsandphoneactivity. This , Smsactivity. Class ); Startactivity (intent1 );}});}}
3. Text message Interface Activity
Public Class Smsactivity Extends Activity {@ override Public Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. SMS); button butsend = (Button) findviewbyid (R. Id. butsend ); Final Edittext myedittext = (Edittext) findviewbyid (R. Id. Mobile ); Final Edittext edittext2 = (Edittext) findviewbyid (R. Id. content); butsend. setonclicklistener ( New Onclicklistener () {@ override Public Void Onclick (view v ){ // Todo auto-generated method stub String mobile =Myedittext. gettext (). tostring (); string content = Edittext2.gettext (). tostring (); smsmanager SMS = Smsmanager. getdefault (); pendingintent sentintent = Pendingintent. getbroadcast (smsactivity. This , 0, New Intent (), 0 ); Try { If (Content. Length ()> 70 ) {List <String> msgs =SMS. dividemessage (content ); For (String MSG: MSGs) {SMS. sendtextmessage (mobile, Null , MSG, sentintent, Null );}} Else {SMS. sendtextmessage (mobile, Null , Content, sentintent, Null );}} Catch (Exception e ){ // Todo: handle exception E. printstacktrace ();} toast. maketext (smsactivity. This , "Message sent successfully", 1000 ). Show ();}});
4. Call Activity
Public Class Phone Extends Activity {@ override Public Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Phone ); Final Edittext myedittext = (Edittext) findviewbyid (R. Id. myedittext); button butphone = (Button) findviewbyid (R. Id. butphone); butphone. setonclicklistener ( New Onclicklistener () {@ override Public Void Onclick (view v ){ // Todo auto-generated method stub If (Myedittext. gettext (). Length ()> 0 ) {Intent myintent = New Intent (intent. action_call, Uri. parse ( "Tel:" + Myedittext. gettext (). tostring (); phone. This . Startactivity (myintent );}}});}}
Download the source code: smsandphone.zip