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();

}

};

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值