/**发送与接收的广播**/
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
private BroadcastReceiver sendMessage = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("TAG", "send。。" + intent.getAction() + "---" + getResultCode() + "---" + getResultData());
//判断短信是否发送成功
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(mContext, "发送失败", Toast.LENGTH_LONG).show();
break;
}
}
};
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("TAG", "receiver。。" + intent.getAction() + "---" + getResultCode() + "---" + getResultData());
//表示对方成功收到短信
Toast.makeText(mContext, "对方接收成功", Toast.LENGTH_LONG).show();
}
};
/**
* 参数说明
* destinationAddress:收信人的手机号码
* scAddress:发信人的手机号码
* text:发送信息的内容
* sentIntent:发送是否成功的回执,用于监听短信是否发送成功。
* DeliveryIntent:接收是否成功的回执,用于监听短信对方是否接收成功。
*/
private void sendSMS(String phoneNumber, String message) {
// ---sends an SMS message to another device---
SmsManager sms = SmsManager.getDefault();
// 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);
//如果短信内容超过70个字符 将这条短信拆成多条短信发送出去
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);
}
}