Android 获取验证码

定义一个类,调用即可,直接看代码。很简单

package com.apicloud.A6970406947389.utils;

import android.os.CountDownTimer;
import android.widget.Button;

import com.apicloud.A6970406947389.R;

/**
 * 时间倒计时
 */
public class TimerCount extends CountDownTimer {
    private Button bnt;

    public TimerCount(long millisInFuture, long countDownInterval, Button bnt) {
        super(millisInFuture, countDownInterval);
        this.bnt = bnt;
    }

    public TimerCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
        bnt.setClickable(true);
        bnt.setBackgroundResource(R.drawable.bt_bg);//设置为正常色
        bnt.setText("获取验证码");
    }

    @Override
    public void onTick(long arg0) {
        // TODO Auto-generated method stub
        bnt.setClickable(false);
        bnt.setBackgroundResource(R.drawable.shape_forgetpwd_bg);//设置为灰色
        bnt.setText(arg0 / 1000 + "秒重新获取");
//        bnt.setText("还剩"+arg0 / 1000 + "秒");
    }
}


在需要的地方直接调用

private TimerCount timer;
timer = new TimerCount(60000,1000,bnt);
getVerifyCode();//执行发送验证码
timer.start();//启动


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
获取手机短信验证码需要使用 Android 的 SMS 模块,代码如下: 1. 在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> ``` 2. 在 Activity 中注册 BroadcastReceiver,用来接收短信: ``` private BroadcastReceiver smsReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) { Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); if (pdus != null && pdus.length > 0) { SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } String messageBody = messages[0].getMessageBody(); // 在这里获取短信验证码并进行处理 } } } } }; ``` 3. 在 Activity 的 onStart() 方法中注册 BroadcastReceiver: ``` @Override protected void onStart() { super.onStart(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); registerReceiver(smsReceiver, intentFilter); } ``` 4. 在 Activity 的 onStop() 方法中取消注册 BroadcastReceiver: ``` @Override protected void onStop() { super.onStop(); unregisterReceiver(smsReceiver); } ``` 注意:在获取短信验证码时要注意权限问题,另外不同手机型号上获取短信验证码的方式可能会有所不同。

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值