System services provided by Android-SmsManager and android File Manager
System services provided by Android-SmsManager)
-- Reprinted with the source: coder-pig
Introduction and illustration of SmsManager:
Of course, to make it easy for you to stick the Code together, you don't have to bother writing the Code:
Copy and paste the file as needed!
1) Call the system's SMS sending function:
Public void SendSMSTo (String phoneNumber, String message) {// determine whether the input phoneNumber is a valid phone number if (PhoneNumberUtils. isGlobalPhoneNumber (phoneNumber) {// Uri. parse ("smsto") Here is the conversion to the specified Uri, fixed syntax Intent intent = new Intent (Intent. ACTION_SENDTO, Uri. parse ("smsto:" + phoneNumber); intent. putExtra ("sms_body", message); startActivity (intent );}}
2) Call the SMS interface provided by the system to send text messages:
Public void sendSMS (String phoneNumber, String message) {// obtain the SMS manager android. telephony. smsManager smsManager = android. telephony. smsManager. getDefault (); // split the text message content (SMS length limit). It seems that the length is limited to 140 characters, that is, // only 70 Chinese characters can be sent, if you want to split up multiple SMS sending parameters, you can write a null List <String> divideContents = smsManager if you do not need to monitor the sending and receiving statuses. divideMessage (message); for (String text: divideContents) {smsManager. sendTextMessage (phoneNumber, null, text, sentPI, deliverPI );}}
PendingIntent that processes the sending status:
// Process the returned sending status String SENT_SMS_ACTION = "SENT_SMS_ACTION"; Intent sentIntent = new Intent (SENT_SMS_ACTION); PendingIntent sentPI = PendingIntent. getBroadcast (context, 0, sentIntent, 0); // register the broadcast receiver context. registerReceiver (new BroadcastReceiver () {@ Override public void onReceive (Context _ context, Intent _ intent) {switch (getResultCode () {case Activity. RESULT_ OK: Toast. makeText (context, "message sent successfully", Toast. LENGTH_SHORT ). show (); break; case SmsManager. RESULT_ERROR_GENERIC_FAILURE: // normal error break; case SmsManager. RESULT_ERROR_RADIO_OFF: // The break is explicitly disabled for wireless broadcasting; case SmsManager. RESULT_ERROR_NULL_PDU: // No pdubreak is provided; case SmsManager. RESULT_ERROR_NO_SERVICE: // break is currently unavailable for the service; }}, new IntentFilter (SENT_SMS_ACTION ));
PendingIntent processing the receipt status:
// Process the returned receipt status String response = "DELIVERED_SMS_ACTION"; // create the Intent deliverIntent = new Intent (DELIVERED_SMS_ACTION) that receives the returned receipt status; PendingIntent deliverPI = PendingIntent. getBroadcast (context, 0, deliverIntent, 0); context. registerReceiver (new BroadcastReceiver () {@ Override public void onReceive (Context _ context, Intent _ intent) {Toast. makeText (context, "the recipient has successfully received", Toast. LENGTH_SHORT ). show () ;}, new IntentFilter (DELIVERED_SMS_ACTION ));
Android smsmanagersendtextmessage I sent a text message. Why did I receive two text messages?
StrMessage is too long to be sent in multiple messages. If you receive two identical text messages, check whether your program logic is correct.
How can I determine the status of multiple messages sent by android ?? <Activity android: name = "SmsActivity" android: label =
SmsManager smsManager = SmsManager. getDefault (); // gets the SMS manager, which is the default SMS manager of the Android system and can be used to send text messages.
List <String> contents = smsManager. divideMessage (content); // multiple SMS messages. This section splits long text messages into multiple messages for sending.
For (String text: contents ){
SmsManager. sendTextMessage (destinationAddress, scAddress, text, sentIntent, deliveryIntent); // send SMS
}
There are two results when you send the SMS. One is whether the SMS is successfully sent, the other is whether the recipient receives the SMS successfully, and the other is not determined by the Android program, SMS messages are sent by SMS base stations, such as mobile phones. We only need to send the SMS to the mobile wireless communication network. If the SMS is successfully sent over the network, mobile will return a signal, which will be captured by the program.
The program uses an Asynchronous Method to capture signals. Because it is impossible to keep the thread on waiting for the returned signal, the last two parameters of sendTextMessage (), sentIntent and deliveryIntent, are used to receive and send status signals.
SentIntent indicates whether the SMS is successfully sent; deliveryIntent indicates whether the receiver has received the SMS Intent. If you do not want to receive the returned message, you can set it to null.
After the Intent is passed in, if the mobile network returns a message indicating that the text message has been sent successfully or failed, the Operating System Broadcasts the Intent asynchronously, and you will know the status of the text message.