android 发送短信验证,Android之发送短信和接收验证码

packagecom.javen.utils;importjava.util.ArrayList;importandroid.app.Activity;importandroid.app.PendingIntent;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.content.IntentFilter;importandroid.telephony.SmsManager;importandroid.widget.Toast;/***@authorJaven

**/

public classSendMessageUtil {/**发送与接收的广播 **/

private static String SENT_SMS_ACTION = "SENT_SMS_ACTION";private static String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";/*** 实现发送短信

*@paramcontext

*@paramtext 短信的内容

*@paramphoneNumber 手机号码*/

public static voidsendMessage(Context context, String text,

String phoneNumber) {

context.registerReceiver(sendMessage,newIntentFilter(SENT_SMS_ACTION));

context.registerReceiver(receiver,newIntentFilter(

DELIVERED_SMS_ACTION));//create the sentIntent parameter

Intent sentIntent = newIntent(SENT_SMS_ACTION);

PendingIntent sentPI= PendingIntent.getBroadcast(context, 0, sentIntent,0);//create the deilverIntent parameter

Intent deliverIntent = newIntent(DELIVERED_SMS_ACTION);

PendingIntent deliverPI= PendingIntent.getBroadcast(context, 0,deliverIntent, 0);

SmsManager smsManager=SmsManager.getDefault();//如果字数超过5,需拆分成多条短信发送

if (text.length() > 70) {

ArrayList msgs =smsManager.divideMessage(text);for(String msg : msgs) {

smsManager.sendTextMessage(phoneNumber,null, msg, sentPI, deliverPI);

}

}else{

smsManager.sendTextMessage(phoneNumber,null, text, sentPI, deliverPI);

}

}private static BroadcastReceiver sendMessage = newBroadcastReceiver() {

@Overridepublic voidonReceive(Context context, Intent intent) {//判断短信是否发送成功

switch(getResultCode()) {caseActivity.RESULT_OK:

Toast.makeText(context,"短信发送成功", Toast.LENGTH_SHORT).show();break;default:

Toast.makeText(context,"发送失败", Toast.LENGTH_LONG).show();break;

}

}

};private static BroadcastReceiver receiver = newBroadcastReceiver() {

@Overridepublic voidonReceive(Context context, Intent intent) {//表示对方成功收到短信

Toast.makeText(context, "对方接收成功", Toast.LENGTH_LONG).show();

}

};

}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值