在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android 短信验证演示 示例源码

android 短信验证演示 示例源码

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:1.59M
  • 下载次数:23
  • 浏览次数:212
  • 发布时间:2018-05-31
  • 实例类别:Android平台开发
  • 发 布 人:zhuxiaosz
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 短信 验证

实例介绍

【实例简介】需要单独申请 短信验证码的key才可以使用 https://www.juhe.cn/docs

【实例截图】

from clipboard

【核心代码】

package com.juhe.captcha;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.thinkland.sdk.sms.SMSCaptcha;
import com.thinkland.sdk.util.BaseData.ResultCallBack;

public class CaptchaActivity extends BaseActivity implements OnClickListener,
		TextWatcher {

	private final String tag = "CaptchaActivity";
	private static final int RETRY_INTERVAL = 60;
	private String phone;
	private String formatedPhone;
	private int time = RETRY_INTERVAL;

	private EditText etIdentifyNum;
	private TextView tvTitle;
	private TextView tvPhone;
	private TextView tvIdentifyNotify;
	private TextView tvUnreceiveIdentify;
	private ImageView ivClear;
	private Button btnSubmit;
	private SMSCaptcha smsCaptcha;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_captcha);
		smsCaptcha = SMSCaptcha.getInstance();
		Intent intent = getIntent();
		this.formatedPhone = intent.getStringExtra("formatedPhone");
		this.phone = intent.getStringExtra("phone");

		btnSubmit = (Button) this.findViewById(R.id.btn_submit);
		btnSubmit.setOnClickListener(this);
		btnSubmit.setEnabled(false);

		tvTitle = (TextView) this.findViewById(R.id.tv_title);
		tvTitle.setText(R.string.smssdk_write_identify_code);

		etIdentifyNum = (EditText) findViewById(R.id.et_put_identify);
		etIdentifyNum.addTextChangedListener(this);

		tvIdentifyNotify = (TextView) findViewById(R.id.tv_identify_notify);
		String text = getResources().getString(
				R.string.smssdk_send_mobile_detail);
		tvIdentifyNotify.setText(Html.fromHtml(text));

		tvPhone = (TextView) findViewById(R.id.tv_phone);
		tvPhone.setText(formatedPhone);

		tvUnreceiveIdentify = (TextView) findViewById(R.id.tv_unreceive_identify);
		String unReceive = getResources().getString(
				R.string.smssdk_receive_msg, time);
		tvUnreceiveIdentify.setText(Html.fromHtml(unReceive));

		tvUnreceiveIdentify.setOnClickListener(this);
		tvUnreceiveIdentify.setEnabled(false);

		ivClear = (ImageView) findViewById(R.id.iv_clear);
		ivClear.setOnClickListener(this);

		countDown();
	}

	private void countDown() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (time-- > 0) {
					String unReceive = CaptchaActivity.this.getResources()
							.getString(R.string.smssdk_receive_msg, time);
					updateTvUnreceive(unReceive);
					Log.i("time is about ", unReceive);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				String unReceive = getResources().getString(
						R.string.smssdk_unreceive_identify_code, time);
				updateTvUnreceive(unReceive);
				time = RETRY_INTERVAL;
			}
		}).start();
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO
		if (s.length() > 0) {
			btnSubmit.setEnabled(true);
			ivClear.setVisibility(View.VISIBLE);
			btnSubmit.setBackgroundResource(R.drawable.smssdk_btn_enable);
		} else {
			btnSubmit.setEnabled(false);
			ivClear.setVisibility(View.GONE);
			btnSubmit.setBackgroundResource(R.drawable.smssdk_btn_disenable);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {

	}

	@Override
	public void afterTextChanged(Editable s) {

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();

		if (id == R.id.btn_submit) {
			//
			showDialog();
			String captcha = etIdentifyNum.getText().toString().trim();
			smsCaptcha.commitCaptcha(phone, captcha, new ResultCallBack() {

				@Override
				public void onResult(int code, String reason, String result) {
					// TODO Auto-generated method stub
					closeDialog();
					Log.v(tag, "code:"   code   ";reason:"   reason);
					showToast(reason);
					if (code == 0) {
						finish();
					}

				}
			});

		} else if (id == R.id.tv_unreceive_identify) {
			//
			showDialog();
		} else if (id == R.id.iv_clear) {
			//
			etIdentifyNum.getText().clear();
		}
	}

	//
	private void showDialog() {
		String strContent = getResources().getString(
				R.string.smssdk_resend_identify_code);
		AlertDialog.Builder builder = new AlertDialog.Builder(
				CaptchaActivity.this);
		builder.setTitle("SMSSDK Demo")
				.setIcon(R.drawable.ic_launcher)
				.setCancelable(false)
				.setMessage(strContent)
				.setPositiveButton(R.string.smssdk_ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								showDialog(getResources()
										.getString(
												R.string.smssdk_get_verification_code_content));

								smsCaptcha.sendCaptcha(phone.trim(),
										new ResultCallBack() {

											@Override
											public void onResult(int code,
													String reason, String result) {
												// TODO Auto-generated method
												// stub
												closeDialog();
												Log.v(tag, "code:"   code
														  ";reason:"   reason);
											}
										});
							}
						})
				.setNegativeButton(R.string.smssdk_back,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
								finish();
							}
						});
	}

	private void showNotifyDialog() {
		String strContent = this.getResources().getString(
				R.string.smssdk_close_identify_page_dialog);
		AlertDialog.Builder builder = new AlertDialog.Builder(
				CaptchaActivity.this);
		builder.setTitle("Captcha Demo")
				.setIcon(R.drawable.ic_launcher)
				.setCancelable(false)
				.setMessage(strContent)
				.setPositiveButton(R.string.smssdk_wait,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						})
				.setNegativeButton(R.string.smssdk_back,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
								finish();
							}
						});
		builder.create().show();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub

		showNotifyDialog();
	}


	private void updateTvUnreceive(final String unReceive) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				tvUnreceiveIdentify.setText(Html.fromHtml(unReceive));
				tvUnreceiveIdentify.setEnabled(false);
			}
		});
	}

}

标签: 短信 验证

实例下载地址

android 短信验证演示 示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警