Android-send SMS and notify the sender, android-sender
1. Permission for sending text messages. We need to add the permission
2. When we send a text message, the system sends a broadcast regardless of whether the message is sent successfully or received by the receiver.
3. Now we can register a broadcast to receive it.
4. The layout file is very simple. There are two EditText and a button in it.
The code below contains comments:
Send broadcast receiver:
Package com. example. fanlei. cutnotedemo; import android. app. activity; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. smsManager; import android. widget. toast;/*** whether the text message has been sent successfully */public class MySmsSendReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (intent. getAction (). equals ("SMS_SEND") {switch (getResultCode () {case Activity. RESULT_ OK: Toast. makeText (context, "sent successfully", Toast. LENGTH_SHORT ). show (); break; case SmsManager. RESULT_ERROR_GENERIC_FAILURE: Toast. makeText (context, "failed to send", Toast. LENGTH_SHORT ). show (); break; case SmsManager. RESULT_ERROR_NO_SERVICE: break; case SmsManager. RESULT_ERROR_NULL_PDU: break; case SmsManager. RESULT_ERROR_RADIO_OFF: break ;}}}}
Receiver Broadcast
Package com. example. fanlei. cutnotedemo; import android. app. activity; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. widget. toast;/*** monitor whether the recipient has received the SMS */public class MySmsDeliverReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (intent. getAction (). equals ("SMS_DELIVER") {switch (getResultCode () {case Activity. RESULT_ OK: Toast. makeText (context, "received by the other party", Toast. LENGTH_SHORT ). show (); break; case Activity. RESULT_CANCELED: break ;}}}}
Main Function
Package com. example. fanlei. cutnotedemo; import android. app. pendingIntent; import android. content. intent; import android. content. intentFilter; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. telephony. smsManager; import android. view. view; import android. widget. editText; import android. widget. toast; import java. util. list; public class MainActivity extends ActionBarActivity {private EditText editText1, editText2; private MySmsSendReceiver mySmsSendReceiver; // broadcast private MySmsDeliverReceiver mySmsDeliverReceiver; // whether the broadcast @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mySmsSendReceiver = new MySmsSendReceiver (); mySmsDeliverReceiver = new MySmsDeliverReceiver (); editText1 = (EditText) findViewById (R. id. editText1); // mobile phone number editText2 = (EditText) findViewById (R. id. editText2); // content // initialization startup Intent sendIntent = new Intent (); sendIntent. setAction ("SMS_SEND"); Intent deliverIntent = new Intent (); deliverIntent. setAction ("SMS_DELIVER"); // after the conditions are met, execute Intent final PendingIntent send = PendingIntent. getBroadcast (MainActivity. this, 0, sendIntent, 0); final PendingIntent deliver = PendingIntent. getBroadcast (MainActivity. this, 0, deliverIntent, 0); // send the SMS button findViewById (R. id. button ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {String phoneNumber = editText1.getText (). toString (); String content = editText2.getText (). toString (); if (phoneNumber. equals ("") {Toast. makeText (MainActivity. this, "the mobile phone number is blank", Toast. LENGTH_SHORT ). show ();} else {// SmsManager sm = SmsManager. getDefault (); // limit on the number of characters in the text message. if it is greater than 70, you need to split the text message to send if (content. length ()> 70) {List <String> contents = sm. divideMessage (content); for (String sms: contents) {sm. sendTextMessage (phoneNumber, null, sms, send, deliver); // send sms} else {sm. sendTextMessage (phoneNumber, null, content, send, deliver); // send SMS }}}); // register the broadcast registerReceiver (mySmsSendReceiver, new IntentFilter ("SMS_SEND ")); registerReceiver (mySmsDeliverReceiver, new IntentFilter ("SMS_DELIVER "));}}
Layout File
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" android: paddingBottom = "@ dimen/plugin" tools: context = ". mainActivity "> <EditText android: layout_width =" match_parent "android: layout_height =" wrap_content "android: id =" @ + id/editText1 "android: hint =" mobile phone number "android: inputType = "phone"/> <EditText android: id = "@ + id/editText2" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_below = "@ + id/editText1" android: layout_alignParentLeft = "true" android: layout_alignParentStart = "true" android: hint = "content"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/button" android: layout_centerVertical = "true" android: layout_centerHorizontal = "true" android: text = "send SMS"/> </RelativeLayout>
You can use 10086 for testing.