private void sendSMS(final String phoneName ,String phoneNumber, String message)
{
// ---sends an SMS message to another device---
SmsManager sms = SmsManager.getDefault();
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
// create the sentIntent parameter
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent,
0);
// create the deilverIntent parameter
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0,
deliverIntent, 0);
// register the Broadcast Receivers
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
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));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(),
"收信人 "+phoneName +"发送成功", Toast.LENGTH_SHORT)
.show();
break;
case Activity.RESULT_CANCELED:
break;
}
}
}, new IntentFilter(DELIVERED_SMS_ACTION));
// if message's length more than 70 ,
// then call divideMessage to dive message into several part ,and call
// sendTextMessage()
// else direct call sendTextMessage()
if (message.length() > 70) {
ArrayList<String> msgs = sms.divideMessage(message);
for (String msg : msgs) {
sms.sendTextMessage(phoneNumber, null, msg, sentPI, deliverPI);
}
} else {
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliverPI);
}
}
阅读(1563) | 评论(0) | 转发(0) |