发表于: 2018-11-26 23:38:52
1 922
一、今天完成的事情
发送短信的两种方法
1.调用系统已有程序发送
Intent sendMessage = new Intent(Intent.ACTION_SENDTO);
sendMessage.setData(Uri.parse("smsto:" + phoneNumberET.getText()));
sendMessage.putExtra("sms_body", messageET.getText().toString());
startActivity(sendMessage);
2.调用系统API发送
主要用到了SmsManager
获取SmsManager
SmsManager smsManager = SmsManager.getDefault();
一些公共方法
smsManager.divideMessage(message);
将短信内容进行拆分,一条短信最多70个中文字符,当超过70个字符时,可以用divideMessage方法将短信拆分,然后分条发送。
sendTextMessage(String destinationAddress, String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent)
sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)
发送短信,sendTextMessage是分条发送,分条接收,sendMultipartTextMessage实际上也是将短信分条发送,只不过我们看到的短信是一整条的,效果如图
具体代码
//分条发送、接收
public void sendMessage(String phoneNumber, String message) {
if (!TextUtils.isEmpty(phoneNumber) && !TextUtils.isEmpty(message)) {
SmsManager smsManager = SmsManager.getDefault();
Intent itSend = new Intent(SMS_SEND_ACTION);
Intent itDeliver = new Intent(SMS_DELIVERED_ACTION);
PendingIntent piSend = PendingIntent.getBroadcast(
CallActivity.this, 0, itSend, 0);
PendingIntent piDeliver = PendingIntent.getBroadcast(
CallActivity.this, 0, itDeliver, 0);
if (message.length() > 70) {
//拆分短信,70字符为一String,将String存入List
ArrayList<String> msgs = smsManager.divideMessage(message);
for (String msg : msgs) {
smsManager.sendTextMessage(phoneNumber, null, msg, piSend, piDeliver);
}
} else {
smsManager.sendTextMessage(phoneNumber, null, message, piSend, piDeliver);
}
} else {
Toast.makeText(CallActivity.this, "号码和内容不能为空", Toast.LENGTH_SHORT).show();
}
}
//合并发送、接收
public void sendMessage2(String phoneNumber, String message) {
if (!TextUtils.isEmpty(phoneNumber) && !TextUtils.isEmpty(message)) {
SmsManager smsManager = SmsManager.getDefault();
Intent itSend = new Intent(SMS_SEND_ACTION);
Intent itDeliver = new Intent(SMS_DELIVERED_ACTION);
PendingIntent piSend = PendingIntent.getBroadcast(
CallActivity.this, 0, itSend, 0);
PendingIntent piDeliver = PendingIntent.getBroadcast(
CallActivity.this, 0, itDeliver, 0);
ArrayList<PendingIntent> piSends = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> piDelivers = new ArrayList<PendingIntent>();
if (message.length() > 70) {
//拆分短信,70字符为一String,将String存入List
ArrayList<String> msgs = smsManager.divideMessage(message);
for (int i = 0; i < msgs.size(); i++) {
piSends.add(piSend);
piDelivers.add(piDeliver);
}
smsManager.sendMultipartTextMessage(phoneNumber, null, msgs, piSends, piDelivers);
} else {
smsManager.sendTextMessage(phoneNumber, null, message, piSend, piDeliver);
}
} else {
Toast.makeText(CallActivity.this, "号码和内容不能为空", Toast.LENGTH_SHORT).show();
}
}
返送完成后通过广播获取发送结果
public class SMSReceiver extends BroadcastReceiver {
private static final String TAG = "SMSReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(CallActivity.SMS_SEND_ACTION)) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Log.i(TAG, "onReceive: 发送成功");
Toast.makeText(context, "发送成功", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.i(TAG, "onReceive: 发送失败");
Toast.makeText(context, "发送失败", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
default:
break;
}
} else if(intent.getAction().equals(CallActivity.SMS_DELIVERED_ACTION)) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "接收成功", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "接收失败", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
default:
break;
}
}
}
}
三、遇到的问题
四、收获
SmsManager
评论