1. In general, our first step is to create this main.xml layout file, which is a good habit:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "vertical" >6 7 <TextView8 Android:layout_width= "Fill_parent"9 Android:layout_height= "Wrap_content"Ten Android:text= "@string/input_number" />//android:text Below are all similar, creating character resources under String.xml, and then leveraging @string/xxx One A <EditText - Android:id= "@+id/et_number" - Android:layout_width= "Match_parent" the Android:layout_height= "Wrap_content" - Android:lines= "1" - Android:numeric= "integer" > - </EditText> + - <TextView + Android:layout_width= "Wrap_content" A Android:layout_height= "Wrap_content" at Android:text= "@string/input_content" /> - - <EditText - Android:id= "@+id/et_content" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" in Android:lines= "4" /> - to <Button + Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:onclick= "Send" * Android:text= "@string/send" /> $ Panax Notoginseng </LinearLayout>
This layout file is as follows:
Below we enter the source code:
1 Packagecn.itcast.sms;2 3 Importjava.util.ArrayList;4 5 Importandroid.app.Activity;6 ImportAndroid.os.Bundle;7 ImportAndroid.telephony.SmsManager;//GSM CDMA 3g8 ImportAndroid.util.Log;9 ImportAndroid.view.View;Ten ImportAndroid.widget.EditText; One ImportAndroid.widget.Toast; A - Public classMainactivityextendsActivity { - Private Static FinalString TAG = "Mainactivity"; the PrivateEditText Metnumber; - PrivateEditText metcontent; - - + - @Override + Public voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.main6); - //metcontent = (EditText) This.findviewbyid (r.id.et_content); - //Metnumber = (EditText) This.findviewbyid (r.id.et_number); - - } - in - to Public voidSend (View view) { +String number =Metnumber.gettext (). toString (). Trim (); -String content =Metcontent.gettext (). toString (). Trim (); the if(". Equals (number) | |" ". Equals (content)) { * //the recipient or content of the user message is not empty. $ //the context is actually a global objectPanax NotoginsengToast.maketext ( This, R.string.error_message, Toast.length_long). Show (); - return ; the}Else{ +LOG.I (TAG, "Send SMS Recipient" +number+ "content" +content); ASmsmanager Smsmanager =Smsmanager.getdefault (); the //Sentintent, deliveryintent the intention of postponing, + //Sentintent Sending reports - //deliveryintent Delivery Report $ //smsmanager.sendtextmessage (number, NULL, content, NULL, NULL); $ - //What is the maximum text length for a message? - //Chinese 70 Kanji English 160 characters thearraylist<string> messages =smsmanager.dividemessage (content); - for(String message:messages) {WuyiSmsmanager.sendtextmessage (number,NULL, Message,NULL,NULL); the } - } Wu } -}
Remark: Smsmanager Smsmanager = smsmanager.getdefault ();
If you simply do not want to be instantiated by another: it is directly defined in this constructor. But then you can't use it yourself.
Singleton mode can only create one instance, and the outer class call can only be an instance of the class that has been created. Code to see you:
Private Eagersingleton () {}//private constructor cannot be created outside the new
Instances of
private static foo foo = new Foo ();//Create an instance of your own in the class
public static Foo getinstance () {//Leave a method for calling your instance outside
}
}
Android (Java) Learning Note 86: Case SMS Transmitter