直接发送短信,在android 系统中有默认的短信读取程序,所以可以使用系统的短信程序来实现发送短信功能。
private void sendSysSMS(String phone_num, String message) {
//在号码前面必须加上smsto:
phone_num = "smsto:" + phone_num;
//实现一个发送短信的Intent ,动作是android.content.Intent.ACTION_SENDTO
Intent sys_send_intent = new Intent(
android.content.Intent.ACTION_SENDTO, Uri.parse(phone_num));
//将短信的内容作为附加数据添加到intent中,名字sms_body
sys_send_intent.putExtra("sms_body", message);
//执行
startActivity(sys_send_intent);
}
设置一个按钮,点击触发这个函数,即可实现使用系统的短信发送程序发送短信。
直接发送短信
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phone_num = editText1.getText().toString();
String sms_text = editText2.getText().toString();
//首先要判断输入的电话号码与输入的内容是否为空,如果为空,则这行else();
if (phone_num.length() > 0 && sms_text.length() > 0) {
sendSmS(phone_num, sms_text);
} else {
Toast.makeText(getBaseContext(),
"Please input both phone number and message", 1000)
.show();
}
}
});
private void sendSmS(String ph_num, String message) {
//获取默认的短信管理类SmsManager:
SmsManager sms = SmsManager.getDefault();
//发送完短信后,需要判断短信是否发送成功以及对方是否已经接受到,这需要实现sendIntent 和deliveryIntent 两个意图。
Intent sentIntent = new Intent(SENT_SMS_ACTION);
//实现一个PendingIntent,发送sentIntent意图
PendingIntent sent_pi = PendingIntent.getBroadcast(this, 0, sentIntent, 0);
//注册实现广播接受者,该广播用于接收发送完成后的广播,并根据不同的编码给出相应的提示信息
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "success", 1000).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "SMS radio failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "SMS radio failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "SMS null PDU failure", 1000).show();
break;
default:
break;
}
}
},new IntentFilter(SENT_SMS_ACTION));
//实现一个PendingIntent,发送deliveryIntent 意图
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliver_pi = PendingIntent.getBroadcast(this, 0, deliverIntent, 0);
//注册实现广播接受者,给广播用于接受提示对方已经收到短信。
registerReceiver(new BroadcastReceiver(){
public void onReceive(Context context,Intent intent){
Toast.makeText(getBaseContext(), "SMS delivered actions", Toast.LENGTH_SHORT).show();
}
},new IntentFilter(DELIVERED_SMS_ACTION));
//如果短信的内容超过70,则分多条进行发送
if (message.length() > 70) {
ArrayList<String> msgs = sms.divideMessage(message);
for (String msg : msgs) {
//发送短信,发送完成后触发sent_pi,对方接受到后触发deliver_pi
sms.sendTextMessage(ph_num, null, msg, sent_pi, deliver_pi);
}
} else {
sms.sendTextMessage(ph_num, null, message, sent_pi, deliver_pi);
}
}
对于上述代码,主要用到了短信发送的SmsManager类:
(1)主要的方法有:static SmsManager getDefault()
用于获取SmsManager 的默认实例,其中,返回值为默认实例。
(2) ArrayList divideMesssage(String text)
当短信长度超过SMS消息的最大长度时,将短信分割成几块。其中,参数text 是初始消息,不能为空。返回值为有序的ArryList ,可以重新组合为初始消息
(3) void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
destinationAddress, 表示消息的目标地址。
scAddress ,表示服务中心的地址。如果为空,则使用当前默认的服务中心地址。
text , 表示消息的主题,即消息要发送的内容
sentIntent, 发送完成后的处理。如果此值不为空,则当消息发送成功或失败后,,该PendingIntent 就被广播。广播中的各项参数分别问:
Activity.RESULT_OK: 表示成功
RESULT_ERROR_GENERIC_FAILURE: 表示普通错误
RESULT_ERROR_RADIO_OFF:表示无线广播被关闭
RESULT_ERROR_NULL_PDU:表示pdu错误
deliveryIntent 如果不为空,当消息成功传递到接收者的PendingIntent广播时,则用于短信回执。
其中,destinationAddress 和text 不能为空,不然会发送异常。
最后,需要在AndroidManifest.xml中申请相应的权限:
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>