短信验证
// An highlighted block
val handler : Handler = object : Handler(){
override fun handleMessage(msg: Message?) {
super.handleMessage(msg)
val toInt = msg!!.arg1.toInt()
login_get.text=toInt.toString()
if(toInt == 0){
login_get.setText("获取验证码")
login_get.setBackgroundColor(Color.BLUE)
login_get.isEnabled = true
time = 60
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
//注册一个事件回调监听,用于处理SMSSDK接口请求的结果
SMSSDK.registerEventHandler(eh)
login_get.setOnClickListener{
val call = login_call.text.toString()
SMSSDK.getVerificationCode("86",call)
login_get.setBackgroundColor(Color.GRAY)
login_get.isEnabled = false
Thread(Mythread()).start()
}
login_login.setOnClickListener{
if(login_yanz.text.toString() == null){
Log.e("#####","验证码不能为空")
}else {
val call = login_call.text.toString()
val yanz = login_yanz.text.toString()
SMSSDK.submitVerificationCode("86",call,yanz)
}
}
}
var time = 60
inner class Mythread:Runnable{
override fun run() {
while (time>0){
val message :Message = Message()
time–
message.arg1 = time
handler.sendMessageDelayed(message,1000)
}
}
}
override fun onDestroy() {
super.onDestroy()
SMSSDK.unregisterEventHandler(eh)
}
val eh = object : EventHandler() {
override fun afterEvent(event: Int, result: Int, data: Any?) {
if(data is Throwable){
Log.e("#####","获取失败")
}else {
if(event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
Log.e("#####","获取成功")
login_get.setBackgroundColor(Color.GRAY)
}else if(SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE == event){
Log.e("#####","验证码正确")
}
}
}
}