標籤:手機簡訊驗證碼登入
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import com.chencheng.model.User;
import com.chencheng.utils.LoginStatus;
import com.chencheng.utils.MyToast;
import com.xinbo.utils.SMSCodeBMobUtils;
public class ShoujiDengruActivity extends ActionBarActivity {
private EditText et_number;
private EditText et_code;
private Button yanzhengma_fasong;
private Button btn_find_back_yanzhengbindengl;
private String number;
private String code;
private String mSmsID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shouji_dengru);
initActionBar();
initUi();
}
private void initUi() {
et_number=(EditText)findViewById(R.id.mobile);
et_code=(EditText)findViewById(R.id.yanzhengma_val);
yanzhengma_fasong=(Button)findViewById(R.id.yanzhengma_fasong);
yanzhengma_fasong.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
number = et_number.getText().toString();
//1、調用請求驗證碼介面
SMSCodeBMobUtils.requestSmsCode(ShoujiDengruActivity.this, number, new RequestSMSCodeListener() {
@Override
public void done(Integer smsId,BmobException ex) {
if(!TextUtils.isEmpty(number)){
if(ex==null){//驗證碼發送成功
// 驗證碼發送成功
mSmsID = smsId+"";
MyToast.makeText(ShoujiDengruActivity.this,"驗證碼發送成功,簡訊id:" + smsId);// 用於查詢本次簡訊發送詳情
daojishi(yanzhengma_fasong);
}
else{
MyToast.makeText(ShoujiDengruActivity.this,"驗證碼發送失敗" );
}
}else{
et_number.setError("請輸入電話號碼");
}
}
});
}
});
btn_find_back_yanzhengbindengl=(Button)findViewById(R.id.btn_find_back_yanzhengbindengl);
btn_find_back_yanzhengbindengl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
signOrLoginByPhoneNumber();
}
});
}
private void initActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setBackgroundDrawable(getResources().getDrawable(
R.color.action_white));
actionBar.setTitle("手機快捷登入");
}
@Override
public boolean onSupportNavigateUp() {
finish();
return super.onSupportNavigateUp();
}
private void signOrLoginByPhoneNumber(){
number = et_number.getText().toString();
code = et_code.getText().toString();
if(!TextUtils.isEmpty(number)&&!TextUtils.isEmpty(code)){
//2、使用手機號和簡訊驗證碼進行一鍵註冊登入
BmobUser.signOrLoginByMobilePhone(this, number, code, new LogInListener<User>() {
@Override
public void done(User user, BmobException e) {
// TODO Auto-generated method stub
if(user!=null){
MyToast.makeText(ShoujiDengruActivity.this,"登入成功");
Log.i("ShoujiDengru", ""+user.getUsername()+"-"+user.getObjectId()+"-"+user.getPassword());
LoginStatus.getInstance().setHasLogin(true);
Intent intent=new Intent(ShoujiDengruActivity.this,MainActivity.class);
intent.putExtra("flag", "hasLogin");
startActivity(intent);
}else{
MyToast.makeText(ShoujiDengruActivity.this,"錯誤碼:"+e.getErrorCode()+",錯誤原因:"+e.getLocalizedMessage());
}
}
});
}else{
MyToast.makeText(ShoujiDengruActivity.this,"請輸入手機號和驗證碼");
}
}
private void daojishi(final Button button) {
new Handler().postDelayed(new Runnable() {
int i=60;
@Override
public void run() {
if(i>0)
{
button.setText(i+"秒");
button.setEnabled(false);
i--;
//Log.e("run", i+"");
new Handler().postDelayed(this, 1000);
}
if(i==0)
{
button.setText( "發送驗證碼");
button.setEnabled(true);
}
}
}, 1000);
}
}
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6F/91/wKioL1WhMFGBFaCNAACfkjwvRCY991.jpg" style="float:none;" title="收入手機號" alt="wKioL1WhMFGBFaCNAACfkjwvRCY991.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6F/91/wKioL1WhMLfgUJDIAAHAVoyZV9U058.jpg" title="手機簡訊收到驗證碼" alt="wKioL1WhMLfgUJDIAAHAVoyZV9U058.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6F/91/wKioL1WhMFOhFdu8AADaDIuuNOw627.jpg" style="float:none;" title="簡訊發送成功" alt="wKioL1WhMFOhFdu8AADaDIuuNOw627.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6F/91/wKioL1WhMFThE1oRAAEvD0d8DIc086.jpg" style="float:none;" title="登陸成功" alt="wKioL1WhMFThE1oRAAEvD0d8DIc086.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6F/94/wKiom1WhLoCinGU3AADsdJFlCHE101.jpg" style="float:none;" title="Bomb上User表添加一列" alt="wKiom1WhLoCinGU3AADsdJFlCHE101.jpg" />
cc_美團項目user模組之用手機驗證碼直接登入實現