如何阻止我的警报对话被立即解雇?

时间:2014-07-01 09:21:06

标签: android

我正在尝试阅读登机牌条码,告知用户我们是否有关于他们航班的详细信息,如果有,为什么不这样做。我正在使用AlertDialogs与用户进行通信,因为Toast通知看起来不够清晰。但是,如果没有用户点击确定,他们会立即解雇。

如何阻止这种情况? 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");
        }
    }
}

致电代码

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);

        }
    });

2 个答案:

答案 0 :(得分:0)

只需尝试以下代码,希望它应该可行。您需要在Dialog Builder中传递上下文。然后,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();

}

如果这不能解决您的问题。然后,您需要显示整个活动代码,因为在对话框显示到屏幕后,您的活动可能很快就会完成。

答案 1 :(得分:0)

这结果是一个多线程问题。 postData方法使用HTTPResponse调用,因此不在主线程上。在显示tyhe对话框后,此方法将在几毫秒内完成,这就是解除alertDialogs的原因。

一个简单的join()调用,让主线程在显示alertDialogs之前等待postData方法完成它的商务。