第二阶段冲刺
我在这个阶段完成了几个功能,有注册成功,加载手机验证码,图片等来实现登录,用语音验证码实现登录,还有几个不同用户种类的对应功能。花了将近一周时间。
遇到的问题基本就是验证码的几个问题和登录验证的问题。
加载手机验证码,图片等来实现登录。
package com.example.shazidouhuiapp.activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.shazidouhuiapp.Bean.User; import com.example.shazidouhuiapp.R; import com.example.shazidouhuiapp.Util.Utils; import org.json.JSONException; import org.json.JSONObject; import cn.bmob.v3.BmobUser; import cn.bmob.v3.exception.BmobException; import cn.bmob.v3.listener.SaveListener; import cn.smssdk.EventHandler; import cn.smssdk.SMSSDK; public class mobloging extends AppCompatActivity { private Button buttonCode,buttonLogin; private EditText editTextPhoneNum,editTextCode,editPassword; private TextView fogetPassword,Register; private String phoneNum,code,pass; private EventHandler eh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initview(); eh = new EventHandler() { @Override public void afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESULT_COMPLETE){ //回调完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { //提交验证码成功 runOnUiThread(new Runnable() { @Override public void run() { pass=editPassword.getText().toString().trim();; phoneNum=editTextPhoneNum.getText().toString().trim(); BmobUser user=new BmobUser(); user.setPassword(pass); user.setUsername(phoneNum); user.login(new SaveListener<User>() { @Override public void done(User user, BmobException e) { if(e==null){ Toast.makeText(mobloging.this, "登陆成功", Toast.LENGTH_SHORT).show(); if(user.getType().equals("student")){ startActivity(new Intent(mobloging.this,studentinterface.class)); } else if(user.getType().equals("teacher")){ startActivity(new Intent(mobloging.this,teacherinterface.class)); } } else { Toast.makeText(mobloging.this, "登陆失败"+e.getMessage(), Toast.LENGTH_LONG).show(); } } }); } }); }else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(mobloging.this,"语音验证发送",Toast.LENGTH_SHORT).show(); } }); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){ //获取验证码成功 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(mobloging.this,"验证码已发送",Toast.LENGTH_SHORT).show(); } }); }else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){ Log.i("test","test"); } }else{ ((Throwable)data).printStackTrace(); Throwable throwable = (Throwable) data; throwable.printStackTrace(); Log.i("1234",throwable.toString()); try { JSONObject obj = new JSONObject(throwable.getMessage()); final String des = obj.optString("detail"); if (!TextUtils.isEmpty(des)){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(mobloging.this,des,Toast.LENGTH_SHORT).show(); } }); } } catch (JSONException e) { e.printStackTrace(); } } } }; // fogetPassword.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // startActivity(new Intent(mobloging.this,resetPassword.class)); // } // }); Register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(mobloging.this,register.class)); } }); //注册一个事件回调监听,用于处理SMSSDK接口请求的结果 SMSSDK.registerEventHandler(eh); buttonCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { phoneNum = editTextPhoneNum.getText().toString(); if(!phoneNum.isEmpty()){ if(Utils.checkTel(phoneNum)){ //利用正则表达式获取检验手机号 // 获取验证码 SMSSDK.getVerificationCode("86", phoneNum); }else{ Toast.makeText(getApplicationContext(),"请输入有效的手机号",Toast.LENGTH_LONG).show(); } }else { Toast.makeText(getApplicationContext(),"请输入手机号",Toast.LENGTH_LONG).show(); return; } phoneNum = editTextPhoneNum.getText().toString(); } }); buttonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { code = editTextCode.getText().toString(); if(!code.isEmpty()){ //提交验证码 SMSSDK.submitVerificationCode("86", phoneNum, code); }else{ Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_LONG).show(); return; } } }); } private void initview() { // private Button buttonCode,buttonLogin; // private EditText editTextPhoneNum,editTextCode,editPassword; buttonCode=findViewById(R.id.verify_getVerify); buttonLogin=findViewById(R.id.loginButton); editPassword=findViewById(R.id.password); editTextCode=findViewById(R.id.verify_verifyCode); editTextPhoneNum=findViewById(R.id.phoneNumber); fogetPassword=findViewById(R.id.fogetPassword); Register=findViewById(R.id.Register); } // 使用完EventHandler需注销,否则可能出现内存泄漏 @Override protected void onDestroy() { super.onDestroy(); SMSSDK.unregisterEventHandler(eh); } }
注册成功的代码
package com.example.shazidouhuiapp.activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.shazidouhuiapp.Bean.User; import com.example.shazidouhuiapp.R; import com.example.shazidouhuiapp.Util.Utils; import org.json.JSONException; import org.json.JSONObject; import cn.bmob.v3.BmobUser; import cn.bmob.v3.exception.BmobException; import cn.bmob.v3.listener.SaveListener; import cn.smssdk.EventHandler; import cn.smssdk.SMSSDK; public class register extends AppCompatActivity { private Button buttonCode,buttonLoginStudent,buttonLoginTeacher; private EditText editTextPhoneNum,editTextCode,editPassword; private String phoneNum,code,pass; private EventHandler eh; private int flag=1;//学生按钮 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); initview(); eh = new EventHandler() { @Override public void afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESULT_COMPLETE){ //回调完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { //提交验证码成功 runOnUiThread(new Runnable() { @Override public void run() { User user=new User(); if(flag==1){ user.setType("student"); user.setUsername(phoneNum); user.setPassword(pass); user.setMobilePhoneNumberVerified(true); user.setMobilePhoneNumber(phoneNum); user.signUp(new SaveListener<User>() { @Override public void done(User user, BmobException e) { if(e==null){ Toast.makeText(register.this, "注册成功", Toast.LENGTH_SHORT).show(); startActivity(new Intent(register.this,splash.class)); } else { Toast.makeText(register.this, "注册失败"+e.getMessage(), Toast.LENGTH_LONG).show(); } } }); } else if(flag==0){ user.setType("teacher"); user.setUsername(phoneNum); user.setPassword(pass); user.setMobilePhoneNumberVerified(true); user.setMobilePhoneNumber(phoneNum); user.signUp(new SaveListener<User>() { @Override public void done(User user, BmobException e) { if(e==null){ Toast.makeText(register.this, "注册成功", Toast.LENGTH_SHORT).show(); startActivity(new Intent(register.this,teacherinterface.class)); } else { Toast.makeText(register.this, "注册失败"+e.getMessage(), Toast.LENGTH_LONG).show(); } } }); } Toast.makeText(register.this,"注册成功",Toast.LENGTH_SHORT).show(); } }); }else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(register.this,"语音验证发送",Toast.LENGTH_SHORT).show(); } }); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){ //获取验证码成功 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(register.this,"验证码已发送",Toast.LENGTH_SHORT).show(); } }); }else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){ Log.i("test","test"); } }else{ ((Throwable)data).printStackTrace(); Throwable throwable = (Throwable) data; throwable.printStackTrace(); Log.i("1234",throwable.toString()); try { JSONObject obj = new JSONObject(throwable.getMessage()); final String des = obj.optString("detail"); if (!TextUtils.isEmpty(des)){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(register.this,des,Toast.LENGTH_SHORT).show(); } }); } } catch (JSONException e) { e.printStackTrace(); } } } }; //注册一个事件回调监听,用于处理SMSSDK接口请求的结果 SMSSDK.registerEventHandler(eh); buttonCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { phoneNum = editTextPhoneNum.getText().toString(); pass=editPassword.getText().toString().trim(); if(!phoneNum.isEmpty()){ if(Utils.checkTel(phoneNum)){ //利用正则表达式获取检验手机号 // 获取验证码 if(!pass.isEmpty()){ if(Utils.checkPssword(pass)){ SMSSDK.getVerificationCode("86", phoneNum); } else { Toast.makeText(register.this, "请输入符合要求的密码", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(register.this, "请输入密码", Toast.LENGTH_SHORT).show(); } }else{ Toast.makeText(getApplicationContext(),"请输入有效的手机号",Toast.LENGTH_LONG).show(); } }else { Toast.makeText(getApplicationContext(),"请输入手机号",Toast.LENGTH_LONG).show(); return; } phoneNum = editTextPhoneNum.getText().toString(); } }); buttonLoginStudent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { code = editTextCode.getText().toString(); if(!code.isEmpty()){ //提交验证码 SMSSDK.submitVerificationCode("86", phoneNum, code); }else{ Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_LONG).show(); return; } } }); buttonLoginTeacher.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { code = editTextCode.getText().toString(); if(!code.isEmpty()){ //提交验证码 flag=0;//老师按钮 SMSSDK.submitVerificationCode("86", phoneNum, code); }else{ Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_LONG).show(); return; } } }); } private void initview() { // private Button buttonCode,buttonLoginStudent,buttonLoginTeacher; // private EditText editTextPhoneNum,editTextCode,editPassword; buttonCode=findViewById(R.id.register_getVerify); buttonLoginStudent=findViewById(R.id.loginButtonsStudent); buttonLoginTeacher=findViewById(R.id.loginButtonTeacher); editTextPhoneNum=findViewById(R.id.register_phoneNumber); editPassword=findViewById(R.id.register_setPassword); editTextCode=findViewById(R.id.register_verifyCode); } // 使用完EventHandler需注销,否则可能出现内存泄漏 @Override protected void onDestroy() { super.onDestroy(); SMSSDK.unregisterEventHandler(eh); } }
语音验证、验证码,用来修改个人信息。
package com.example.shazidouhuiapp.activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.shazidouhuiapp.Bean.User; import com.example.shazidouhuiapp.R; import com.example.shazidouhuiapp.Util.Utils; import org.json.JSONException; import org.json.JSONObject; import cn.bmob.v3.BmobQuery; import cn.bmob.v3.BmobUser; import cn.bmob.v3.exception.BmobException; import cn.bmob.v3.listener.SaveListener; import cn.bmob.v3.listener.UpdateListener; import cn.smssdk.EventHandler; import cn.smssdk.SMSSDK; public class resetPassword extends AppCompatActivity { private Button buttonCode,buttonLogin; private EditText editTextPhoneNum,editTextCode,editPassword; private String phoneNum,code,pass,oldpass; private EventHandler eh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_passwordreset); initview(); eh = new EventHandler() { @Override public void afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESULT_COMPLETE){ //回调完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { //提交验证码成功 runOnUiThread(new Runnable() { @Override public void run() { pass=editPassword.getText().toString().trim();; phoneNum=editTextPhoneNum.getText().toString().trim(); BmobUser user=new BmobUser(); user.setUsername(phoneNum); user.setPassword(pass); user.update(new UpdateListener() { @Override public void done(BmobException e) { if(e==null){ Toast.makeText(resetPassword.this, "修改成功", Toast.LENGTH_SHORT).show(); startActivity(new Intent(resetPassword.this,splash.class)); } else{ Toast.makeText(resetPassword.this, "修改失败"+e.getMessage(), Toast.LENGTH_LONG).show(); } } }); } }); }else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(resetPassword.this,"语音验证发送",Toast.LENGTH_SHORT).show(); } }); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){ //获取验证码成功 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(resetPassword.this,"验证码已发送",Toast.LENGTH_SHORT).show(); } }); }else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){ Log.i("test","test"); } }else{ ((Throwable)data).printStackTrace(); Throwable throwable = (Throwable) data; throwable.printStackTrace(); Log.i("1234",throwable.toString()); try { JSONObject obj = new JSONObject(throwable.getMessage()); final String des = obj.optString("detail"); if (!TextUtils.isEmpty(des)){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(resetPassword.this,des,Toast.LENGTH_SHORT).show(); } }); } } catch (JSONException e) { e.printStackTrace(); } } } }; //注册一个事件回调监听,用于处理SMSSDK接口请求的结果 SMSSDK.registerEventHandler(eh); buttonCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { phoneNum = editTextPhoneNum.getText().toString(); pass=editPassword.getText().toString(); if(!phoneNum.isEmpty()){ if(Utils.checkTel(phoneNum)){ //利用正则表达式获取检验手机号 // 获取验证码 if(!pass.isEmpty()){ if(Utils.checkPssword(pass)){ SMSSDK.getVerificationCode("86", phoneNum); } else { Toast.makeText(resetPassword.this, "请输入符合要求的密码", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(resetPassword.this, "请输入密码", Toast.LENGTH_SHORT).show(); } }else{ Toast.makeText(getApplicationContext(),"请输入有效的手机号",Toast.LENGTH_LONG).show(); } }else { Toast.makeText(getApplicationContext(),"请输入手机号",Toast.LENGTH_LONG).show(); return; } phoneNum = editTextPhoneNum.getText().toString(); } }); buttonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { code = editTextCode.getText().toString(); if(!code.isEmpty()){ //提交验证码 SMSSDK.submitVerificationCode("86", phoneNum, code); }else{ Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_LONG).show(); return; } } }); } private void initview() { // private Button buttonCode,buttonLogin; // private EditText editTextPhoneNum,editTextCode,editPassword; buttonCode=findViewById(R.id.reset_getVerify); buttonLogin=findViewById(R.id.resetButton); editPassword=findViewById(R.id.newPassword); editTextCode=findViewById(R.id.reset_verifyCode); editTextPhoneNum=findViewById(R.id.reset_phoneNumber); } // 使用完EventHandler需注销,否则可能出现内存泄漏 @Override protected void onDestroy() { super.onDestroy(); SMSSDK.unregisterEventHandler(eh); } }