简体   繁体   English

如何阻止我的警报对话框立即被关闭?

[英]How to stop my Alert Dialog from being immediately dismissed?

I'm trying to read a boarding pass barcode to inform the user if we have details on their flight and if so why not. 我正在尝试读取登机牌条形码,以告知用户我们是否有他们的航班详细信息,如果有,为什么呢。 I'm using AlertDialogs to communicate with the user as Toast notifications did not appear clearly enough. 我正在使用AlertDialogs与用户通信,因为Toast通知的显示不够清晰。 However they dismiss as soon as they are called without the user clicking ok. 但是,在用户未单击“确定”的情况下,它们将在调用后立即关闭。

How do I stop this? 如何停止呢? Is onActivityResult the wrong place to put this code? onActivityResult是放置此代码的错误位置吗?

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String boardingPassString = intent
                    .getStringExtra("SCAN_RESULT");
            Log.d("Scan Result", "contents: " + boardingPassString);
            String flightNumber = dataProcessor.decodeFlightNumber(boardingPassString);

            Builder dialogBuilder = new AlertDialog.Builder(this);
            String isFlightOld = isFlightOld(boardingPassString);
            if(isFlightOld.equals(CURRENT))
            {
                Log.d("Block", "Current");
                postData(flightNumber);
            }
            else if(isFlightOld.equals(TOO_NEW))
            {
                dialogBuilder.setTitle(R.string.dialog_title_new);

                dialogBuilder.setMessage(R.string.dialog_msg_new1 + flightNumber + R.string.dialog_msg_new2);
                dialogBuilder.setPositiveButton(android.R.string.ok, null);
                AlertDialog alert = dialogBuilder.create();
                alert.show();

                Log.d("Block", getResources().getString(R.string.dialog_title_new));
            }
            else if(isFlightOld.equals(OLD))
            {
                dialogBuilder.setTitle(R.string.dialog_title_old);
                dialogBuilder.setMessage(R.string.dialog_msg_old1 + flightNumber + R.string.dialog_msg_old2);
                dialogBuilder.setPositiveButton(android.R.string.ok, null);
                AlertDialog alert = dialogBuilder.create();
                alert.show();
                Log.d("Block", getResources().getString(R.string.dialog_title_old));
            }
            else
            {
                dialogBuilder.setTitle(R.string.dialog_title_error);
                dialogBuilder.setMessage(R.string.dialog_msg_error).show();
                dialogBuilder.setPositiveButton(android.R.string.ok, null);
                AlertDialog alert = dialogBuilder.create();
                alert.show();
                Log.d("Block", getResources().getString(R.string.dialog_title_error));
            }


        } else if (resultCode == RESULT_CANCELED) {
            Log.d("Scan Result", "RESULT_CANCELED");
        }
    }
}

Calling Code 呼叫码

scanButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // Testing shortcut
            // =================================
            // getXMLFlightDetails("US729");
            // ==================================

            // Uncomment to return Barcode scanning!!!
            // =============================================
            Intent intent = new Intent(getApplicationContext(),
                    CaptureActivity.class);
            intent.setAction("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "PDF417_MODE");
            intent.putExtra("SAVE_HISTORY", false);
            startActivityForResult(intent, 0);

        }
    });

Just try the following code, hopefully it should work. 只需尝试以下代码,希望它可以工作。 You need to pass the context in Dialog Builder. 您需要在Dialog Builder中传递上下文。 Then the Android OS will know which activity the dialog is attached to and where it should be popped. 然后,Android操作系统将知道对话框附加到的活动以及应将其弹出的位置。

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String boardingPassString = intent
                    .getStringExtra("SCAN_RESULT");
            Log.d("Scan Result", "contents: " + boardingPassString);
            String flightNumber = dataProcessor.decodeFlightNumber(boardingPassString);

        Builder dialogBuilder = new AlertDialog.Builder(this);
        String isFlightOld = isFlightOld(boardingPassString);
        if(isFlightOld.equals(CURRENT))
        {
            Log.d("Block", "Current");
            postData(flightNumber);
        }
        else if(isFlightOld.equals(TOO_NEW))
        {
            displayAlert(R.string.dialog_msg_new1 + flightNumber + R.string.dialog_msg_new2);
            Log.d("Block", getResources().getString(R.string.dialog_title_new));
        }
        else if(isFlightOld.equals(OLD))
        {
            displayAlert(R.string.dialog_msg_old1 + flightNumber + R.string.dialog_msg_old2);
            Log.d("Block", getResources().getString(R.string.dialog_title_old));
        }
        else
        {
            displayAlert(R.string.dialog_msg_error);
            Log.d("Block", getResources().getString(R.string.dialog_title_error));
        }


    } else if (resultCode == RESULT_CANCELED) {
        Log.d("Scan Result", "RESULT_CANCELED");
    }
}
}



displayAlert(String message)
{

  AlertDialog.Builder alertDialog = new AlertDialog.Builder(MyActivity.this);
  alertDialog.setTitle(getResources().getString(R.string.alert_title));
  alertDialog.setMessage(message);

  alertDialog.setPositiveButton(getResources().getString(R.string.ok),
    new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) 
      {
         //your action here
      }
  });


  alertDialog.setNegativeButton(getResources().getString(R.string.ok),
      new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
           // Your Action here      
      }
  });
  alertDialog.show();

}

If this does not solve your problem. 如果这样不能解决您的问题。 Then you need to show ur entire activity code, as it may be possible that your activity is getting finished soon after the dialog is displayed to the screen. 然后,您需要显示您的整个活动代码,因为可能会在对话框显示到屏幕后不久完成您的活动。

This turned out to be a multi-threading issue. 原来这是一个多线程问题。 The method postData used a HTTPResponse call so was not on the main thread. 方法postData使用HTTPResponse调用,因此不在主线程上。 This method would finish a few milliseconds after tyhe dialog had been displayed and was what was dismissing the alertDialogs. 此方法将在显示对话框之后的几毫秒内完成,而这正是关闭alertDialogs的原因。

A simple join() call to get the main thread to wait for the postData method to do it's buisness before displaying the alertDialogs was all it took. 一个简单的join()调用即可使主线程等待postData方法完成其业务,然后再显示alertDialogs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM