/** * Created by 崗哥 on 2016/12/17. *下面使用的ShowToast是自己寫的一個類,當然你們改成Toast就行了 *還有就是如果需要使用Mob發送驗證碼找回密碼的話,肯定首先要導入他們的jar文件 *官網:http://www.mob.com/downloadDetail/SMS/android */ public class WriteVerificationCode extends AppCompatActivity { EditText code; Button resultButton; String phone;//需要接受驗證碼的手機號碼 TextView regetTextviewCode;//再次獲取驗證碼 TextView log, timelog, phoneTextview; ImageView back; String APPKEY = "自己申請的appkey"; String APPSECRET = "自己去官網申請密碼"; private String PASSWORD = "[a-zA-Z0-9]{6,16}"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.write_verification_code); Intent intent = getIntent(); phone = intent.getStringExtra("phone"); initSMSSDK(); SMSSDK.getVerificationCode("86", phone);//發送短信驗證碼到手機號 initView(); initData(); timer.start(); } private void initView() { regetTextviewCode = (TextView) findViewById(R.id.regetcode); back = (ImageView) findViewById(R.id.write_back); code = (EditText) findViewById(R.id.verfication_code_edittext); resultButton = (Button) findViewById(R.id.submit_verfication); phoneTextview = (TextView) findViewById(R.id.verfication_phone); log = (TextView) findViewById(R.id.write_log); timelog = (TextView) findViewById(R.id.submit_log); } private void initData() { back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { WriteVerificationCode.this.finish(); } }); phoneTextview.setText("+86 " + phone); log.setText(Html.fromHtml("我們已經發送<font color='#45C01A'>驗證碼</font>短信到整個號碼:")); resultButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String codeString = code.getText().toString().trim(); SMSSDK.submitVerificationCode("86", phone, codeString); } }); regetTextviewCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ShowToast.showToast(WriteVerificationCode.this, "請求已發送"); SMSSDK.getVerificationCode("86", phone);//發送短信驗證碼到手機號 timer.start(); } }); } /** * 使用計時器來限定驗證碼 * 在發送驗證碼的過程 不可以再次申請獲取驗證碼 在指定時間之後沒有獲取到驗證碼才能重新進行發送 * 這裏限定的時間是30s */ private CountDownTimer timer = new CountDownTimer(30000, 1000) { @Override public void onTick(long millisUntilFinished) { timelog.setText(("接收短信大約需要" + millisUntilFinished / 1000) + "秒"); } @Override public void onFinish() { regetTextviewCode.setEnabled(true); regetTextviewCode.setVisibility(View.VISIBLE); } }; @Override protected void onDestroy() { super.onDestroy(); //防止使用短信驗證 產生內存溢出問題 SMSSDK.unregisterAllEventHandler(); } private void initSMSSDK() { //初始化短信驗證 SMSSDK.initSDK(this, APPKEY, APPSECRET); //註冊短信回調 SMSSDK.registerEventHandler(new EventHandler() { @Override public void afterEvent(int event, int result, Object data) { if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交成功 try { if (data != null) { HashMap<String, Object> hashMap = (HashMap) data;//提交驗證碼之後得到返回的數據(返回的數據是手機,和國家代碼) String getphone = (String) hashMap.get("phone"); if (getphone.equals(phone)) { Message message = new Message();//驗證成功 message.what = 0; handler.sendMessage(message); } } else { Message message = new Message();//提交的驗證碼錯誤 message.what = 1; handler.sendMessage(message); } } catch (Exception e) { Message message = new Message();//提交的驗證碼錯誤 message.what = 1; handler.sendMessage(message); } // startActivity(new Intent(WriteVerificationCode.this,ModifyPassword.class)); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { if (result == SMSSDK.RESULT_COMPLETE) { boolean smart = (Boolean) data; if (smart) { Message message = new Message();//智能驗證成功 message.what = 2; handler.sendMessage(message); new Handler().postDelayed(new Runnable() { public void run() { // startActivity(new Intent(WriteVerificationCode.this,ModifyPassword.class)); //WriteVerificationCode.this.finish(); } }, 2000); } else { } } } else { Message message = new Message();//得到驗證碼錯誤 message.what = 3; handler.sendMessage(message); } } }); } /** * 需要開啓一個主線程來顯示提示 */ Handler handler = new Handler() { @Override public void handleMessage(Message msg) { int code1 = msg.what; switch (code1) { case 0: Toast.makeText(WriteVerificationCode.this, "驗證成功", Toast.LENGTH_SHORT).show(); //執行驗證成功的操作 break; case 1: ShowToast.showToast(WriteVerificationCode.this, "您提交的驗證碼有誤"); break; case 2: resultButton.setText("該號碼爲可信任號碼"); ShowToast.showToast(WriteVerificationCode.this, "智能驗證成功,即將爲您跳轉頁面"); showDialog(); break; case 3: ShowToast.showToast(WriteVerificationCode.this, "驗證碼獲取失敗"); break; } super.handleMessage(msg); } };
}
佈局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/forgetheader"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/write_back" android:src="@mipmap/feed_back_back"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="填寫驗證碼" android:textSize="23sp" android:layout_marginLeft="40dp" android:textColor="@color/white"/> </android.support.v7.widget.Toolbar> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:orientation="vertical" android:background="@color/white"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_gravity="center_horizontal" android:textColor="#969696" android:textSize="16sp" android:id="@+id/write_log" android:text="我們已經發送驗證碼短信到整個號碼:"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:textSize="18sp" android:id="@+id/verfication_phone" android:textColor="@android:color/black" android:text="+86 18223082696"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:hint="填寫驗證碼" android:inputType="number" android:textSize="18sp" android:id="@+id/verfication_code_edittext" android:background="@drawable/button" android:layout_marginTop="40dp" android:maxLength="4" android:layout_marginLeft="20dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="接收短信大約需要48秒" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:id="@+id/submit_log" android:textColor="#969696"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:id="@+id/regetcode" android:textColor="@color/forgetbutton" android:visibility="invisible" android:text="重新獲取驗證碼"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginRight="20dp" android:background="@drawable/forgetbuttonstyle" android:textColor="@color/white" android:text="下一步" android:id="@+id/submit_verfication" android:textSize="18sp"/> </LinearLayout> </LinearLayout>