暂停Android应用程序直到提供输入

时间:2017-07-05 05:09:05

标签: java android sql servlets

我已经创建了一个应用程序,在开始时我们要求输入用户名和安全密码,然后让用户使用该应用程序。

目前,我的应用程序在弹出框中一个接一个地询问用户名和安全问题,但在后台应用程序也会运行。

应用程序正在等待用户输入用户名和安全问题的答案,因为在那个时间之后,哪个帖子网址请求无法获取请求" ans1 [0]"变量未设置。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    securityquestion(); // **This part calls the pop up dialog boxes** 
    statusTxtView = (TextView) findViewById(R.id.status_text);
    //Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    intentFilter
            .addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    intentFilter
            .addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, getMainLooper(), null);
   // super.onPause();
    startRegistrationAndDiscovery();
    //super.onResume();
    servicesList = new WiFiDirectServicesList();
    getFragmentManager().beginTransaction()
            .add(R.id.container_root, servicesList, "services").commit();

}

    public void securityquestion(){
        final EditText txtUrl = new EditText(this);
        final EditText txtUrl2 = new EditText(this);
        final String[] ans1 = {""};
        final String[] ans2 = {""};
// Set the default text to a link of the Queen
        int num = getradomquestionumber();
        String messge = "";
        if(num==1)
        {
            txtUrl.setHint("First Pet name");
            messge = "What is your first pet name?";
        }else{
            txtUrl.setHint("Mother's maiden name");
            messge = "What is your mother's maiden name?";
        }
        txtUrl2.setHint("User Name");


        new AlertDialog.Builder(this)
                .setTitle("Security Question")
                .setMessage(messge)
                .setView(txtUrl)
                .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String url = txtUrl.getText().toString();
                        ans2[0] = url;
                        // moustachify(null, url);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .show();

        new AlertDialog.Builder(this)
                .setTitle("User Name")
                .setMessage("Enter UserName")
                .setView(txtUrl2)
                .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String url = txtUrl.getText().toString();
                        ans1[0] = url;
                        // moustachify(null, url);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .show();

        HashMap<String , String> postDataParams = new HashMap<String, String>();
        postDataParams.put("key", String.valueOf(num));
        postDataParams.put("key2", ans1[0]);
        response = performPostCall("http://10.19.23.2/NEWS/SecurityQuestion.php", postDataParams);
        System.out.println("response -------------- "+response);
        if(response == ans2[0]){

        }else{
            System.exit(0);
        }
    }

目前我无法从服务器获得响应,因为应用程序不会等待用户的响应。如何暂停应用程序以便首先显示对话框,接受用户输入,然后在身份验证后进一步继续应用程序。

请你帮我解答你的建议。

2 个答案:

答案 0 :(得分:0)

你需要做这样的事情:

  1. 您的主页面已创建(初始化需要的内容)并已启动
  2. 您的主页面会打开弹出对话框
  3. 用户输入名称和问题,点击
  4. 此时,您发出服务器请求(在后台)。在这里,您的主页面或弹出窗口必须显示一些等待的指示器,如旋转器
  5. 如果服务器成功,则停止旋转并继续
  6. 如果服务器不成功,您停止旋转,向用户输出一些错误,让她再试一次
  7. 此外,执行代码不应该在onCreate()中完成,而是在onStart()中完成,然后只要服务器以肯定的答案回复你。有关这些非常基本的内容,请参阅activity lifecycle

答案 1 :(得分:0)

通过更改方法 securityquestion()尝试此操作,如下所示:

public void securityquestion(){
    final EditText txtUrl = new EditText(this);
    final EditText txtUrl2 = new EditText(this);
    final String[] ans1 = {""};
    final String[] ans2 = {""};
// Set the default text to a link of the Queen
        final int num = getradomquestionumber();
        String messge = "";
        if(num==1)
        {
            txtUrl.setHint("First Pet name");
            messge = "What is your first pet name?";
        }else{
            txtUrl.setHint("Mother's maiden name");
            messge = "What is your mother's maiden name?";
        }
        txtUrl2.setHint("User Name");
    new AlertDialog.Builder(LoginActivity.this)
            .setTitle("User Name")
            .setMessage("Enter UserName")
            .setView(txtUrl2)
            .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String url = txtUrl2.getText().toString();
                    ans1[0] = url;
                    // moustachify(null, url);

                    //ask question and get result
                    new AlertDialog.Builder(LoginActivity.this)
                            .setTitle("Security Question")
                            .setMessage(messge)
                            .setView(txtUrl)
                            .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    String url = txtUrl.getText().toString();
                                    ans2[0] = url;
                                    // moustachify(null, url);

                                    //Now call or make server request
                                    HashMap<String , String> postDataParams = new HashMap<String, String>();
                                    postDataParams.put("key", String.valueOf(num));
                                    postDataParams.put("key2", ans1[0]);
                                    response = performPostCall("http://10.19.23.2/NEWS/SecurityQuestion.php", postDataParams);
                                    System.out.println("response -------------- "+response);
                                    if(response == ans2[0]){

                                    }else{
                                        System.exit(0);
                                    }
                                }
                            })
                            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                }
                            })
                            .show();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            })
            .show();
}