Sending Text MessagesTo send a text message, use sendTextMessage from the SMS Manager, passing in the address (phone number) of your recipient and the text message you want to send:SmsManager smsManager = SmsManager.getDefault();String sendTo = "5551234";String myMessage = "Android supports programmatic SMS messaging!"; smsManager.sendTextMessage(sendTo, null, myMessage, null, null);The second parameter can be used to specify the SMS service center to use. If you enter null, the default service center for the device’s carrier will be used.The final two parameters let you specify Intents to track the transmission and successful delivery of your messages. To react to these Intents, create and register Broadcast Receivers, as shown in the section “Tracking and Confirming SMS Message Delivery.”The Android debugging bridge supports sending SMS messages among multiple emulator instances. To send an SMS from one emulator to another, specify the port number of the target emulator as the “to” address when sending a new message. Android will route your message to the target emulator instance, where it will be received as a normal SMS.Tracking and Confirming SMS Message DeliveryTo track the transmission and delivery success of your outgoing SMS messages, implement and register Broadcast Receivers that listen for the actions you specify when creating the Pending Intents you pass in to the sendTextMessage method.The first Pending Intent parameter is fired when the message is either successfully sent or fails to send. The result code for the Broadcast Receiver that receives this Intent will be one of the following:► Activity.RESULT_0K — To indicate a successful transmission► SmsManager.RESULT_ERR0R_GENERic_FAiLURE — To indicate a nonspecific failure► SmsManager.RESULT_ERR0R_RADi0_0FF — To indicate the phone radio is turned off► SmsManager.RESULT_ERR0R_NULL_PDU — To indicate a PDU (protocol description unit) failure► SmsManager.RESULT_ERR0R_N0_SERViCE — To indicate that no cellular service is currently availableThe second Pending Intent parameter is fired only after the recipient receives your SMS message. The following code snippet shows the typical pattern for sending an SMS and monitoring the success of its transmission and delivery.String SENT_SMS_ACTI0N = "com.paad.smssnippets.SENT_SMS_ACTI0N";String DELIVERED_SMS_ACTI0N = "com.paad.smssnippets.DELIVERED_SMS_ACTI0N";// Create the sentIntent parameterIntent sentlntent = new Intent(SENT_SMS_ACTI0N);Pendinglntent sentPI = PendingIntent.getBroadcast(getApplicationContext(),0,sentIntent,PendingIntent.FLAG_UPDATE_CURRENT);// Create the deliveryIntent parameterIntent deliverylntent = new Intent(DELIVERED_SMS_ACTI0N);PendingIntent deliverPI =PendingIntent.getBroadcast(getApplicationContext(),0,deliveryIntent,PendingIntent.FLAG_UPDATE_CURRENT);// Register the Broadcast Receivers registerReceiver(new BroadcastReceiver() {©Overridepublic void onReceive(Context _context, Intent _intent){String resultText = "UNKNOWN";switch (getResultCode()) {
đang được dịch, vui lòng đợi..
