第一种:调用系统短信接口直接发送短信;主要代码如下:
/*** 直接调用短信接口发短信
*@paramphoneNumber
*@parammessage*/
public voidsendSMS(String phoneNumber,String message){//获取短信管理器
android.telephony.SmsManager smsManager =android.telephony.SmsManager.getDefault();smsManager.sendTextMessage(phoneNumber,null, message, sentPI, deliverPI);
}
第二种:如果短信的长度超过了发送了发送限制,那么可以使用如下的方法:
/*** 直接调用短信接口发短信
*@paramphoneNumber
*@parammessage*/
public voidsendMSM(String phoneNumber, String message) {
android.telephony.SmsManager smsManager=android.telephony.SmsManager
.getDefault();
ArrayList divideContents =smsManager.divideMessage(message);
ArrayList sentIntents = new ArrayList();
ArrayList deliverIntents = new ArrayList();for(String text : divideContents) {
sentIntents.add(sentPI);
deliverIntents.add(deliverPI);
}
smsManager.sendMultipartTextMessage(phoneNumber,null, divideContents,
sentIntents, deliverIntents);
}
别忘了权限,记得在AndroidMannifest.xml中设置权限:
这种方法可以监控发送状态和对方接收状态。
处理返回的发送状态:
//处理返回的发送状态
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent= newIntent(SENT_SMS_ACTION);
PendingIntent sentPI= PendingIntent.getBroadcast(context, 0, sentIntent,0);//register the Broadcast Receivers
context.registerReceiver(newBroadcastReceiver() {
@Overridepublic voidonReceive(Context _context, Intent _intent) {switch(getResultCode()) {caseActivity.RESULT_OK:
Toast.makeText(context,"短信发送成功", Toast.LENGTH_SHORT)
.show();break;caseSmsManager.RESULT_ERROR_GENERIC_FAILURE:break;caseSmsManager.RESULT_ERROR_RADIO_OFF:break;caseSmsManager.RESULT_ERROR_NULL_PDU:break;
}
}
},new IntentFilter(SENT_SMS_ACTION));
处理返回的接收状态 :
//处理返回的接收状态
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";//create the deilverIntent parameter
Intent deliverIntent = newIntent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI= PendingIntent.getBroadcast(context, 0,
deliverIntent,0);
context.registerReceiver(newBroadcastReceiver() {
@Overridepublic voidonReceive(Context _context, Intent _intent) {
Toast.makeText(context,"收信人已经成功接收", Toast.LENGTH_SHORT)
.show();
}
},new IntentFilter(DELIVERED_SMS_ACTION));
发送短信参数说明:
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
-- destinationAddress:目标电话号码
-- scAddress:短信中心号码,测试可以不填
-- text: 短信内容
-- sentIntent:发送 -->中国移动 --> 中国移动发送失败 --> 返回发送成功或失败信号 --> 后续处理 即,这个意图包装了短信发送状态的信息
-- deliveryIntent: 发送 -->中国移动 --> 中国移动发送成功 --> 返回对方是否收到这个信息 --> 后续处理 即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。
接下来是一个Demo,android版本为4.4
首页界面:
AndroidManifest.xml文件:
androidManifest.xml
MainActivity.java
packagecom.example.smstest;importjava.util.ArrayList;importjava.util.List;importandroid.os.Bundle;importandroid.app.Activity;importandroid.app.PendingIntent;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.content.IntentFilter;importandroid.telephony.SmsManager;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.LinearLayout;importandroid.widget.TextView;importandroid.widget.Toast;public class MainActivity extendsActivity {privateLinearLayout llt;privateContext context;private int PHONENUMBER=1001;private int SMSCONTENT=1002;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llt=(LinearLayout)findViewById(R.id.main_layout);
context=this;
addText("电话号码");
addTextField(PHONENUMBER);
addText("短信内容");
addTextField(SMSCONTENT);
Button btn= addBtn("发送短信");
btn.setOnClickListener(newOnClickListener() {
@Overridepublic voidonClick(View arg0) {//处理返回的发送状态
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent= newIntent(SENT_SMS_ACTION);
PendingIntent sentPI= PendingIntent.getBroadcast(context, 0, sentIntent,0);//register the Broadcast Receivers
context.registerReceiver(newBroadcastReceiver() {
@Overridepublic voidonReceive(Context _context, Intent _intent) {switch(getResultCode()) {caseActivity.RESULT_OK:
Toast.makeText(context,"短信发送成功", Toast.LENGTH_SHORT)
.show();break;caseSmsManager.RESULT_ERROR_GENERIC_FAILURE:break;caseSmsManager.RESULT_ERROR_RADIO_OFF:break;caseSmsManager.RESULT_ERROR_NULL_PDU:break;
}
}
},newIntentFilter(SENT_SMS_ACTION));//处理返回的接收状态
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";//create the deilverIntent parameter
Intent deliverIntent = newIntent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI= PendingIntent.getBroadcast(context, 0,
deliverIntent,0);
context.registerReceiver(newBroadcastReceiver() {
@Overridepublic voidonReceive(Context _context, Intent _intent) {
Toast.makeText(context,"收信人已经成功接收", Toast.LENGTH_SHORT)
.show();
}
},newIntentFilter(DELIVERED_SMS_ACTION));//获取短信手机号码
EditText ett=(EditText)llt.findViewById(PHONENUMBER);
String phoneNumber=ett.getText().toString();//获得短信信息
EditText smsEtt=(EditText)llt.findViewById(SMSCONTENT);
String smscontent=smsEtt.getText().toString();//创建一个短信管理器
android.telephony.SmsManager smsManager =android.telephony.SmsManager.getDefault();
ArrayList divideContents=smsManager.divideMessage(smscontent);
ArrayList sentIntents = new ArrayList();
ArrayList deliverIntents = new ArrayList();for(String text : divideContents){
sentIntents.add(sentPI);
deliverIntents.add(deliverPI);
}
smsManager.sendMultipartTextMessage(phoneNumber,null, divideContents, sentIntents, deliverIntents);
}
});
}
Button addBtn(String text){
Button btn=newButton(context);
btn.setText(text);
llt.addView(btn);returnbtn;
}void addTextField(intid){
EditText ett=newEditText(context);
ett.setId(id);
llt.addView(ett);
}voidaddText(String text){
TextView tv=newTextView(context);
tv.setText(text);
llt.addView(tv);
}
}
MainActivity.java