首页 > 解决方案 > 如果数据库中存在名称,如何停止进程android

问题描述

我有一个页面,新用户可以在我的应用程序中注册,以便他可以使用我的应用程序。
我在这个页面中有两个字段,一个是电话号码,第二个是他的密码。
我也有一个按钮,如果他按下此按钮,他将移至另一页。

如果用户在移动之前单击此按钮继续到另一个页面,我尝试在此页面中执行的操作我尝试检查此电话号码或他的设备 ID 是否已经在我的数据库中。因此,如果他的电话号码或 ID 设备已经存在于我的数据库中。用户将显示“电话号码已可用”之类的消息并停止继续到另一个页面或停止此过程。在这种情况下,继续按钮将不起作用。那就是我试图做的。

现在按钮中的代码是这样的:

        findViewById(R.id.buttonContinue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = CountryData.countryAreaCodes[spinner.getSelectedItemPosition()];
                if (number.isEmpty() || number.length() < 6) {
                    editText.setError("Valid number is required");
                    editText.requestFocus();
                    return;
                }

                String phoneNumber = "+" + code + number;
                String UserCountry = spinner.getSelectedItem().toString();
                Intent intent = new Intent(RegistrationPage.this, VerifyPhoneActivity.class);
                intent.putExtra("phonenumber",phoneNumber);
                intent.putExtra("phone", "+"+code+ phone);
                startActivity(intent);

            }
        });

我是如何尝试这样做的。我通过 volley 和 php 文件建立一个连接,以查看此电话号码是否在 db 中可用。

private void Regist(){
        String URL_REGIST = "https://xxxxxxxxxxxxx/registers.php";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGIST,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String Response = jsonObject.getString("response");
                            Toast.makeText(RegistrationPage.this,"Test"+ Response, Toast.LENGTH_LONG).show();



                        } catch (JSONException e) {
                            e.printStackTrace();
                       Toast.makeText(RegistrationPage.this,"This device already exists", Toast.LENGTH_LONG).show();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                        Toast.makeText(RegistrationPage.this, "Register Error! " + error.toString(), Toast.LENGTH_SHORT).show();

                    }
                })

        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("deviceID",deviceID);

                return params;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);


    }

并在意图之前的按钮中添加此方法:

        findViewById(R.id.buttonContinue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

Regist();//================here like that 


                String code = CountryData.countryAreaCodes[spinner.getSelectedItemPosition()];
                if (number.isEmpty() || number.length() < 6) {
                    editText.setError("Valid number is required");
                    editText.requestFocus();
                    return;
                }

                String phoneNumber = "+" + code + number;
                String UserCountry = spinner.getSelectedItem().toString();
                Intent intent = new Intent(RegistrationPage.this, VerifyPhoneActivity.class);
                intent.putExtra("phonenumber",phoneNumber);
                intent.putExtra("phone", "+"+code+ phone);
                startActivity(intent);

            }
        });

现在就像我的代码正在运行一样,我可以看到“此设备已存在”的消息。但是用户仍然会移动到另一个页面,所以如果这条消息来了,我怎么能让它停止呢?

PHP代码

<?php
    include 'connt.php';
$deviceID = $_POST['deviceID'];

 $CheckSQL = "SELECT * FROM user WHERE deviceID='$deviceID'";
 
 $check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
 
 if(isset($check)){

 echo 'phone Already Exist';

 }
        mysqli_close($con);
    
    
?>

标签: android

解决方案


问题是在你检查之后,无论如何你仍然有代码可以继续,所以创建一个名为的新方法,continue 然后将 continue 代码放入其中,如下所示:

String code =   CountryData.countryAreaCodes[spinner.getSelectedItemPosition()];
            if (number.isEmpty() || number.length() < 6) {
                editText.setError("Valid number is required");
                editText.requestFocus();
                return;
            }

            String phoneNumber = "+" + code + number;
            String UserCountry = 
spinner.getSelectedItem().toString();
            Intent intent = new Intent(RegistrationPage.this, 
VerifyPhoneActivity.class);
            intent.putExtra("phonenumber",phoneNumber);
            intent.putExtra("phone", "+"+code+ phone);
            startActivity(intent);

从按钮 onClicklistener 中删除它。然后在Regist();try 函数里面把新continue方法放在 toast 之后。它应该工作


推荐阅读