如何在显示对话框时暂停Android应用程序?

时间:2014-03-18 18:57:45

标签: android android-webview alertdialog onclicklistener

嘿,我正在制作一个简单的webview的Android应用程序。我想将用户重定向到某些网址上的浏览器。我想以对话框的形式显示一条消息,提醒用户他们正被重定向到他们的浏览器。用户将单击“确定”,然后重定向用户。

以下代码就是我所拥有的。问题是应用程序继续重定向,而不等待用户在对话框上单击“确定”。我希望应用程序仅在用户单击“确定”时暂停并继续任何活动。

            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { /*DialogInterface called while setting the AlertDialog Buttons */


                public void onClick(DialogInterface dialog, int which) {

                    //Here you can perform functions of Alert Dialog Buttons as shown
                    switch (which){
                        case DialogInterface.BUTTON_POSITIVE:

                            //I want the app to pause here.
                            //MainActivity.this.onPause(); does not work
                            break;
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Title");// Set the Title of Alert Dialog
            builder.setMessage("We are taking you to the admin page.").setPositiveButton("OK",dialogClickListener).show();

谢谢,我不想使用wait()因为我希望我的用户通过点击按钮来确认发生了什么

1 个答案:

答案 0 :(得分:0)

试试这个:

public class MyClass extends Activity {

    private boolean mustScan = true;

    protected void onCreate(Bundle b) {
        if (mustScan) {
            // Just scan
            // Note: If you use a Thread with a while loop, use this:
            // while (true) {if (mustScan) {} }
        }
    }

    // This is the method you use to show the Dialog
    private void showResults (SomeResults here) {
        // Show the dialog
        // ...
        mustScan = false;
        // Also, in the OnClickListener of the buttons add "mustScan = true;"
    }