The diagram above is a code structure diagram.
Now let's look at the specific code.
Send.java
Package cn.com.sms.send;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import android.app.Activity;
Import android.app.PendingIntent;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;
The public class Send extends the activity {private String message;
private String number;
Private EditText EditText;
Private EditText editText2;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
EditText = (edittext) This.findviewbyid (R.id.number);
EditText2 = (edittext) This.findviewbyid (r.id.message);
Button button = (button) This.findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {number = Edittext.gettext (). toString ();
message = Edittext2.gettext (). toString ();
In Logcat You can view information about number and message log.i ("number", number);
LOG.I ("message", message); /* Get the system default information manager, it is important to note that Smsmanager is Android.telephony.SmsManager; This is related to the version we use and should be used before Android 2.0 Android.teleph
The version after Ony.gsm.SmsManager *android 2.0 should be android.telephony.SmsManager.
* * Smsmanager Smsmanager = Smsmanager.getdefault ();
/*pendingintent.getbroadcast returns a Pendingintent object for broadcast, similar to the invocation of Content.sendbroadcast ();
* * Pendingintent paintent = pendingintent.getbroadcast (send.this, 0, New Intent ("Sms_sent"), 0);
Pendingintent deliveryintent = pendingintent.getbroadcast (send.this, 0, New Intent ("sms_delivered"), 0);
Smsmanager.dividemessage There are times when text messages exceed the number of words, and we need this method to help us split text messages.
arraylist<string> smses = smsmanager.dividemessage (message); Iterator<string> iterator = Smses.iterator ();
while (Iterator.hasnext ()) {String temp = Iterator.next ();
Send SMS Smsmanager.sendtextmessage (number, NULL, temp, paintent, deliveryintent); ///Pop up a floating box to show the content, Toast.length_long represents the length of time the Floating box Toast.maketext (send.this, "SMS Send Complete", Toast.length_long).
Show ();
}
});
}
}
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=" fill_parent "android:layout_height=" Fill_parent " > <textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" Welcome to use SMS transmitter, please enter the phone number "/> <edittext android:id=" @+id/number "android:layout_width=" Fill_parent "Android:layout" _height= "Wrap_content" android:hint= "Here Enter the phone number"/> <textview android:layout_width= "Fill_parent" Android: layout_height= "Wrap_content" android:text= "Welcome to use SMS transmitter, please input SMS content"/> <edittext android:id= "a Ndroid:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:minlines= "3" android:hint= "Enter SMS here Rong "/> <button android:id=" @+id/button "android:layout_width=" wrap_content "android:layout_height=" Wrap_ Content "android:text=" send "
/> </LinearLayout>
Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
package=" cn.com.sms.send "
android:versioncode=" 1 "
android:versionname=" 1.0 ">
< USES-SDK android:minsdkversion= "8"/>
<uses-permission android:name= "Android.permission.SEND_SMS" > </uses-permission>
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". Send "
android:label=" @string/app_name ">
<intent-filter>
<action android:name=" Android.intent.action.MAIN "/>
<category android:name=" Android.intent.category.LAUNCHER "/>"
</intent-filter>
</activity>
</application>
</manifest>
The final effect diagram is:
As with the phone applet, there is a need to turn on two AVD to perform functional testing.
broken up read:
The main class of text messaging applications is Smsmanager. Android.telephony.gsm.SmsManager should be used before Android 2.0
Afterwards should use Android.telephony.SmsManager;
Smsmanager Smsmanager = Smsmanager.getdefault ();
To get the system default information manager
Smsmanager.sendtextmessage (destinationaddress, scaddress, text, sentintent, deliveryintent)
--destinationaddress: Target phone number
-Scaddress: The service provider's message center number (such as China Mobile's SMS Center number), the test can not fill.
--Text: SMS Content
--sentintent: Send--> China Mobile--> send failure--> return send success or failure signal--> subsequent processing that is, this intent to package the message sent status information
--deliveryintent: Send--> China Mobile--> to send a successful--> back to the other side whether received this information--> follow-up processing that is: this intention to wrap the message is received by the other side of the status information (the supplier has sent success, but the other party did not have received).
public static Pendingintent Getbroadcast (context context, int requestcode, Intent Intent, int flags)
Returns a pendingintent for broadcast, similar to calling the Context.sendbroadcast () function
Requestcode, not yet.
Intent is a intent for broadcasting.
Flag are: Flag_one_shot, Flag_no_create, Flag_cancel_current, flag_update_current for setting up a new pendingintent is used once, if none is created, Cancels current, update current, and so on.
In addition, we also want to declare in the Androidmanifest.xml SMS send permission.
<uses-permission android:name= "Android.permission.SEND_SMS"/>
Sometimes, when we have two AVD to simulate texting, we find that the program is not working properly. The system prompts US no DNS servers found and cannot find the DNS service. This is generally due to the fact that your computer is not linked to the network.
Send SMS:
Smsmanager smsmgr = Smsmanager.getdefault ();
Smsmgr.sendtextmessage (address, NULL, message, NULL, NULL);
Display the Write Text interface:
Uri Smstouri = Uri.parse ("smsto://10086");
Intent mintent = new Intent (Android.content.Intent.ACTION_SENDTO, Smstouri);
StartActivity (mintent);
Send e-mail:
Intent i = new Intent (intent.action_send);
I.putextra (Intent.extra_email, address);
I.putextra (intent.extra_subject, filename);
I.putextra (Intent.extra_stream, Uri.parse ("file://" + filename));
I.settype ("Text/csv");
StartActivity (Intent.createchooser (i, "EMail File"));
Upgrade version:
The code adds a listener for the broadcast receiver. Detailed code is as follows
Package cn.com.sms.send;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import android.app.Activity;
Import android.app.PendingIntent;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;
The public class Send extends the activity {private String message;
private String number;
Private EditText EditText;
Private EditText editText2;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
EditText = (edittext) This.findviewbyid (R.id.number);
EditText2 = (edittext) This.findviewbyid (r.id.message);
Button button = (button) This.findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {Number = ed
Ittext.gettext (). toString ();
message = Edittext2.gettext (). toString ();
In Logcat You can view information about number and message log.i ("number", number);
LOG.I ("message", message); /* Get the system default information manager, it is important to note that Smsmanager is Android.telephony.SmsManager; This is related to the version we use and should be used before Android 2.0 Android.teleph
The version after Ony.gsm.SmsManager *android 2.0 should be android.telephony.SmsManager.
* * Smsmanager Smsmanager = Smsmanager.getdefault ();
/*pendingintent.getbroadcast returns a Pendingintent object for broadcast, similar to the invocation of Content.sendbroadcast ();
* * Pendingintent paintent = pendingintent.getbroadcast (send.this, 0, New Intent ("Sms_sent2"), 0);
Pendingintent deliveryintent = pendingintent.getbroadcast (send.this, 0, New Intent ("Sms_delivered2"), 0); Sign up for a broadcastreceiver, when there's a intentfilter intent that matches it., the method is triggered Registerreceiver (new Broadcastreceiver () {@Override public void onreceive (conte
XT context, Intent Intent) {int resultcode = Getresultcode (); Switch (resultcode) {case Activity.RESULT_OK:Toast.makeText (Getbasecontext (), "message sent successfully Oh,", Toa St.
Length_long). Show ();
Break
Default:Toast.makeText (Getbasecontext (), "message delivery failed Oh,", Toast.length_long). Show ();
}}, New Intentfilter ("Sms_sent2")); Registerreceiver (New Broadcastreceiver () {@Override public void onreceive (context conte
XT, Intent Intent) {Toast.maketext (Getbasecontext (), "Deliveryintent", Toast.length_long). Show ();
LOG.I ("message receiver whether to see information", "read");
}, New Intentfilter ("Sms_delivered2")); Smsmanager.dividemessage There are times when text messages exceed the number of words, and we need this method to help us split text messages.
arraylist<string> smses = smsmanager.dividemessage (message);
Iterator<string> iterator = Smses.iterator ();
while (Iterator.hasnext ()) {String temp = Iterator.next ();
Send SMS Smsmanager.sendtextmessage (number, NULL, temp, paintent, deliveryintent); ///Pop up a floating box to show the content, Toast.length_long represents the length of time the Floating box Toast.maketext (send.this, "SMS Send Complete", Toast.length_long).
Show ();
}
});
}
}
Main.xml is the same as Androidmanifest.xml and the previous code.
Registerreceiver () is used to register a broadcast recipient. This method is defined in the content.
Public abstract Intent Registerreceiver (broadcastreceiver receiver,intentfilter filter); If the system queries to a broadcast that satisfies the filter, They will teach receiver to deal with it. is typically handled in its onreceive () method.
If you are not actively registering through registerreceiver () in your code, you will configure it from Androidmanifest.xml, as follows
<receiver android:name= "class name" >
<intent-filter>
<action android:name= "action attribute of intent parameter in receiver" />
</intent-filter>
</receiver>
Notice here that the activity tag and the receiver tag are peers in the configuration file.
In the simulator to send Chinese will receive a garbled problem, but in the real machine, there will be no garbled situation. So developers only need to develop the normal message function, do not need to encode the conversion.