Android发送短信-创新互联
首先做好布局,然后在“发送”做点击事件,如下所示:

String phoneNumber = editPhone.getText().toString().trim();
String message = editMessage.getText().toString().trim();
sendSMS(phoneNumber,message);
private void sendSMS(String phoneNumber, String message) {
String SENT_SMS_ACTION="SENT_SMS_ACTION";
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
sendIntent = new Intent(SENT_SMS_ACTION);
deliverIntent = new Intent(DELIVERED_SMS_ACTION);
//返回发送的状态
sentPI = PendingIntent.getBroadcast(this, 0, sendIntent,
0);
this.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(MainActivity.this,
"短信发送成功", Toast.LENGTH_SHORT)
.show();
editPhone.setText("");
editMessage.setText("");
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));
//返回接收的状态
deliverPI = PendingIntent.getBroadcast(this, 0,
deliverIntent, 0);
this.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
Toast.makeText(MainActivity.this,
"收信人已经成功接收", Toast.LENGTH_SHORT)
.show();
}
}, new IntentFilter(DELIVERED_SMS_ACTION));
//发送短信
SmsManager smsManager =SmsManager.getDefault();
ArrayList divideMessage = smsManager.divideMessage(message);
for(String text:divideMessage){
smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI);
}
} 这样就好了,最后不要忘记了在清单文件中配置短信的权限
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:Android发送短信-创新互联
文章地址:http://www.scyingshan.cn/article/dcdgjd.html


咨询
建站咨询
