The Android API provides the Smsmanager class to process SMS. The sendtextmessage (num, NULL, content, pend, NULL) functions are sent, as described below:
Main classes of SMS involved Smsmanager
Implementation SMS is mainly used in the Smsmanager class, which inherits from the Java.lang.Object class, and below we introduce the main members of the class.
Public Method:
1, arraylist<string> dividemessage (String text)
When a text message exceeds the maximum length of the SMS message, the SMS is divided into several pieces.
Parameter: text--initial message, cannot be empty
Return value: Ordered Arraylist<string>, can be combined back to the initial message
2. Static Smsmanager Getdefault ()
Gets the default instance of the Smsmanager.
Return value: default instance of Smsmanager
3. void Senddatamessage (String destinationaddress, String scaddress, short destinationport, byte[] Data,pendingintent Sentintent, Pendingintent deliveryintent)
Sends an SMS based data to the specified application port.
Parameters:
1), the destination address of the destinationaddress--message
2), scaddress--Service center address or null use the current default SMSC
3) The target port number of the destinationport--message
4, the body of the data--message, that is, the message to send the data
5), sentintent--if not NULL, when the message successfully sent or failed this pendingintent broadcast. The result code is ACTIVITY.RESULT_OK, or one of Result_error_generic_failure, Result_error_radio_off, RESULT_ERROR_NULL_PDU represents an error. The corresponding result_error_generic_failure,sentintent may include an additional "error code" containing a radio broadcast technology-specific value that is usually useful only when repairing a failure.
Each SMS-based application control detection sentintent. If the sentintent is empty, the caller will detect all unknown applications, which will cause a smaller number of SMS to be sent at the time of Detection.
6), deliveryintent--if not NULL, when the message successfully transmitted to the recipient of this pendingintent broadcast.
Exception: Throws IllegalArgumentException exception if destinationaddress or data is empty.
4. void Sendmultiparttextmessage (String destinationaddress, String scaddress, arraylist<string> parts,arraylist <PendingIntent> sentintents, arraylist<pendingintent> deliverintents)
Sends a partial text based on SMS, which has been split into the correct size by calling Dividemessage (String text).
Parameters:
1), the destination address of the destinationaddress--message
2), scaddress--Service center address or null use the current default SMSC
3), parts--ordered Arraylist<string>, can be combined into the initial message
4), sentintents--and Senddatamessage method, but here is a group of pendingintent
5), deliverintents--and Senddatamessage method, but here is a group of pendingintent
Exception: Throws IllegalArgumentException exception if destinationaddress or data is empty.
5. void Sendtextmessage (String destinationaddress, String scaddress, string text, Pendingintent sentintent, Pendingintent deliveryintent)
Sends an SMS based text. The meaning of the parameter is no longer described as the existing one before the exception.
Constant:
- public static final int result_error_generic_failure represents a common error with a value of 1 (0x00000001)
- public static final int Result_error_no_service indicates that the service is currently unavailable with a value of 4 (0x00000004)
- public static final int RESULT_ERROR_NULL_PDU indicates no PDU provided, value 3 (0x00000003)
- The public static final int Result_error_radio_off indicates that the wireless broadcast was explicitly shut down with a value of 2 (0x00000002)
- The public static final int status_on_icc_free represents free space with a value of 0 (0x00000000)
- public static final int Status_on_icc_read is received and read with a value of 1 (0x00000001)
- The public static final int status_on_icc_sent represents a store and has been sent, with a value of 5 (0x00000005)
- The public static final int Status_on_icc_unread represents a receive but unread value of 3 (0x00000003)
- The public static final int status_on_icc_unsent represents a store but is sent, with a value of 7 (0x00000007)
First: Call the system SMS interface to send SMS directly; The main code is as follows:
/**
* Direct SMS interface Send SMS *
*
@param phonenumber
* @param message
/public
void Sendsms (String PhoneNumber, String message) {
//Get SMS Manager
Android.telephony.SmsManager Smsmanager = Android.telephony.SmsManager
. Getdefault ();
Split SMS content (SMS length limit)
list<string> dividecontents = smsmanager.dividemessage (message);
for (String text:dividecontents) {
smsmanager.sendtextmessage (phonenumber, NULL, text, SENTPI,
Deliverpi);
}
}
Second: The system to send SMS function; The main code is as follows:
/**
* The system text message function
* @param phonenumber
* @param messages/
public
void Dosendsmsto (String Phonenumber,string message) {
if (Phonenumberutils.isglobalphonenumber (PhoneNumber)) {
Intent Intent = new Intent (Intent.action_sendto, Uri.parse ("Smsto:" +phonenumber));
Intent.putextra ("Sms_body", message);
StartActivity (intent);
}
Here is the first method, the first method can monitor the sending status and the other receiving State to use more.
The status code returned is processed as follows:
Process returned send status String sent_sms_action = "Sent_sms_action";
Intent sentintent = new Intent (sent_sms_action);
Sentpi= Pendingintent.getbroadcast (this, 0, sentintent, 0); Register the broadcast receivers This.registerreceiver (new Broadcastreceiver () {@Override public void
OnReceive (Context _context, Intent _intent) {switch (Getresultcode ()) {case ACTIVITY.RESULT_OK:
Toast.maketext (mainactivity.this, "SMS sent successfully", Toast.length_short). Show ();
Break
Case SmsManager.RESULT_ERROR_GENERIC_FAILURE:break;
Case SmsManager.RESULT_ERROR_RADIO_OFF:break;
Case SmsManager.RESULT_ERROR_NULL_PDU:break;
}}, new Intentfilter (sent_sms_action));
Processing the returned receive status String delivered_sms_action = "Delivered_sms_action";
Create the deilverintent parameter Intent deliverintent = new Intent (delivered_sms_action); Deliverpi = PendIngintent.getbroadcast (this, 0, deliverintent, 0); This.registerreceiver (New Broadcastreceiver () {@Override public void onreceive (context _context, Intent _inte
NT) {Toast.maketext (Mainactivity.this, "Recipient has successfully received", Toast.length_short). Show ();
}, New Intentfilter (delivered_sms_action));
The following is the encapsulation of the above code
1, send the message
Import android.app.PendingIntent;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.telephony.SmsManager;
Import java.util.ArrayList;
Import java.util.List;
/** * Created by Javen on 2016-03-15.
* * Public class Smsmethod {private static Smsmethod Msmsmsmethod;
/* Custom action constant, as broadcast intent filter identification constant/public static String Sms_send_actioin = "Sms_send_actioin";
public static String sms_delivered_action = "Sms_delivered_action";
/* Set up two Mservicereceiver objects, as a class member variable * * Private smsreceiver msendsmsreceiver, Mdeliveredsmsreceiver;
Private context Mcontext;
Private Smsmethod (Context context) {Mcontext=context;
Registerreceiver (); public static Smsmethod getinstance (context context) {if (msmsmsmethod==null) {synchronized (Smsmethod.clas
s) {if (msmsmsmethod==null) {msmsmsmethod=new Smsmethod (context);
}} return Msmsmsmethod;
}
/*** Registration/public void Registerreceiver () {/* Custom intentfilter for sent_sms_actioin Receiver * * Intentfilter Mfilter
01;
MFilter01 = new Intentfilter (sms_send_actioin);
Msendsmsreceiver = new Smsreceiver ();
Mcontext.registerreceiver (Msendsmsreceiver, MFilter01);
/* Custom Intentfilter for delivered_sms_action Receiver * * MFilter01 = new Intentfilter (sms_delivered_action);
Mdeliveredsmsreceiver = new Smsreceiver ();
Mcontext.registerreceiver (Mdeliveredsmsreceiver, MFilter01); public void Unregisterreceiver () {/* Unregister custom Receiver */if (msendsmsreceiver!=null) {Mcontext.unregiste
Rreceiver (Msendsmsreceiver);
} if (Mdeliveredsmsreceiver!=null) {mcontext.unregisterreceiver (mdeliveredsmsreceiver); } public void SendMessage (String strdestaddress,string strmessage) {/* Create Smsmanager object/Smsmanager Smsmana
GER = Smsmanager.getdefault (); try {/* Create a custom action constant for Intent (for pendingintent parameters) * * Intent Itsend= new Intent (sms_send_actioin);
Intent itdeliver = new Intent (sms_delivered_action); /* sentintent parameters for broadcast messages received after transmission pendingintent * * * pendingintent msendpi = pendingintent.getbroadcast (mcontext, 0, itSend, 0
); /* Deliveryintent parameter is received after delivery of broadcast information pendingintent/pendingintent Mdeliverpi = pendingintent.getbroadcast (mContext, 0, it
Deliver, 0);
list<string> dividecontents = Smsmanager.dividemessage (strmessage); for (String text:dividecontents) {/* Send SMS text messages, note the reciprocal of the two pendingintent parameters */smsmanager.sendtextmessage (strdes
Taddress, NULL, text, msendpi, MDELIVERPI);
}}catch (Exception e) {e.printstacktrace (); } public void SendMessage2 (String strdestaddress,string strmessage) {arraylist<pendingintent> Sentpendingi
ntents = new arraylist<pendingintent> ();
arraylist<pendingintent> deliveredpendingintents = new arraylist<pendingintent> (); * * Set up Smsmanager object/Smsmanager SmsmanageR = Smsmanager.getdefault ();
try {/* Create a custom action constant for Intent (for pendingintent parameters) * * Intent itsend = new Intent (sms_send_actioin);
Intent itdeliver = new Intent (sms_delivered_action); /* sentintent parameters for broadcast messages received after transmission pendingintent * * * pendingintent msendpi = pendingintent.getbroadcast (mcontext, 0, itSend, 0
); /* Deliveryintent parameter is received after delivery of broadcast information pendingintent/pendingintent Mdeliverpi = pendingintent.getbroadcast (mContext, 0, it
Deliver, 0);
arraylist<string> msmsmessage = Smsmanager.dividemessage (strmessage);
for (int i = 0; i < msmsmessage.size (); i++) {Sentpendingintents.add (I, msendpi);
Deliveredpendingintents.add (i, MDELIVERPI); * * Send SMS messages, pay attention to the reciprocal of the two pendingintent parameters * * Smsmanager.sendmultiparttextmessage (Strdestaddress,null,msmsmessage, S
entpendingintents,deliveredpendingintents);
}catch (Exception e) {e.printstacktrace ();
}
}
}
2, SMS send the status of monitoring
Package com.javen.sms.receiver;
Import android.app.Activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.telephony.SmsManager;
Import Android.widget.Toast;
/** * Created by Javen on 2016-03-15. */public class Smsreceiver extends broadcastreceiver{@Override the public void onreceive (context context, Intent Intent {if (Intent.getaction (). Equals (Smsmethod.sms_send_actioin)) {try{/* android.content.BroadcastReceive
R.getresultcode () method * *//retrieve The current result code, as set by the previous receiver.
Switch (Getresultcode ()) {Case Activity.RESULT_OK:System.out.println ("SMS sent successfully");
Toast.maketext (context, "SMS Send Success", Toast.length_short). Show ();
Break
Case SMSMANAGER.RESULT_ERROR_GENERIC_FAILURE:SYSTEM.OUT.PRINTLN ("SMS Send Failure");
Toast.maketext (context, "SMS Send Failure", Toast.length_short). Show (); Break
Case SmsManager.RESULT_ERROR_RADIO_OFF:break;
Case SmsManager.RESULT_ERROR_NULL_PDU:break;
}}catch (Exception e) {e.printstacktrace (); ' Else If ' (Intent.getaction (). Equals (Smsmethod.sms_delivered_action)) {/* Android.content.BroadcastReceiver.get
ResultCode () method */Switch (Getresultcode ()) {Case ACTIVITY.RESULT_OK:SYSTEM.OUT.PRINTLN ("message delivered");
Toast.maketext (context, "message delivered", Toast.length_short). Show ();
Break
Case SMSMANAGER.RESULT_ERROR_GENERIC_FAILURE:SYSTEM.OUT.PRINTLN ("message Not Delivered");
/* Message Not Delivered/Toast.maketext (context, "message Not Delivered", Toast.length_short). Show ();
Break
Case SmsManager.RESULT_ERROR_RADIO_OFF:break;
Case SmsManager.RESULT_ERROR_NULL_PDU:break;
}
}
}
}
Test code:
public void Sendtextmessage (the view view) {
smsmethod.getinstance (this). SendMessage ("xxxx", "Test SMS ...") ");
}
public void Sendmultiparttextmessage (the view view) {
smsmethod.getinstance (this). SendMessage2 ("xxxx", "Test SMS wwww ...") Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... Test SMS Wwww ... ");
}
@Override
protected void OnPause () {
smsmethod.getinstance (this). Unregisterreceiver ();
Super.onpause ();
}
Don't forget the question of permission:
<uses-permission android:name= "Android.permission.SEND_SMS"/>
above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud-dwelling community.