首页 > 解决方案 > 如何通过电子邮件在 android 应用程序中实现 otp 功能

问题描述

我想在我的 android 应用程序中实现 OTP 功能。在此应用程序中,输入电子邮件 ID 后,它会检查用户是否存在。如果用户电子邮件 ID 不存在,用户将收到一次性密码密钥 (OTP)。收到 OTP 后,用户将能够登录应用程序。我需要做什么才能在我的应用程序上实现这一目标?

标签: androidone-time-password

解决方案


试试这个代码:

添加java类

//This method would confirm the otp
private void confirmOtp() throws JSONException {
    //Creating a LayoutInflater object for the dialog box
    LayoutInflater li = LayoutInflater.from(this);
    //Creating a view to get the dialog box
    View confirmDialog = li.inflate(R.layout.dialog_confirm, null);

    //Initizliaing confirm button fo dialog box and edittext of dialog box
    buttonConfirm = (AppCompatButton) confirmDialog.findViewById(R.id.buttonConfirm);
    editTextConfirmOtp = (EditText) confirmDialog.findViewById(R.id.editTextOtp);

    //Creating an alertdialog builder
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    //Adding our dialog box to the view of alert dialog
    alert.setView(confirmDialog);

    //Creating an alert dialog
    final AlertDialog alertDialog = alert.create();

    //Displaying the alert dialog
    alertDialog.show();

    //On the click of the confirm button from alert dialog
    buttonConfirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Hiding the alert dialog
            alertDialog.dismiss();

            //Displaying a progressbar
            final ProgressDialog loading = ProgressDialog.show(MainActivity.this, "Authenticating", "Please wait while we check the entered code", false,false);

            //Getting the user entered otp from edittext
            final String otp = editTextConfirmOtp.getText().toString().trim();

            //Creating an string request
            StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.CONFIRM_URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            //if the server response is success
                            if(response.equalsIgnoreCase("success")){
                                //dismissing the progressbar
                                loading.dismiss();

                                //Starting a new activity
                                startActivity(new Intent(MainActivity.this, Success.class));
                            }else{
                                //Displaying a toast if the otp entered is wrong
                                Toast.makeText(MainActivity.this,"Wrong OTP Please Try Again",Toast.LENGTH_LONG).show();
                                try {
                                    //Asking user to enter otp again
                                    confirmOtp();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            alertDialog.dismiss();
                            Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String, String>();
                    //Adding the parameters otp and username
                    params.put(Config.KEY_OTP, otp);
                    params.put(Config.KEY_USERNAME, username);
                    return params;
                }
            };

            //Adding the request to the queue
            requestQueue.add(stringRequest);
        }
    });
}

添加另一个 API 类

public class Config {
//URLs to register.php and confirm.php file
public static final String REGISTER_URL = "http://simplifiedcoding.16mb.com/AndroidOTP/register.php";
public static final String CONFIRM_URL = "http://simplifiedcoding.16mb.com/AndroidOTP/confirm.php";

//Keys to send username, password, phone and otp
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_PHONE = "phone";
public static final String KEY_OTP = "otp";

//JSON Tag from response from server
public static final String TAG_RESPONSE= "ErrorMessage";
}

推荐阅读