无法关闭runOnUiThread

时间:2019-09-23 16:13:31

标签: android barcode-scanner

我正在使用Google的Vision扫描QR码。我从这个link获得了帮助。我想从QR码获取值后显示AlertDialog。如果我直接在receiveDetections()方法中显示alertdialog,则会收到“无法创建循环播放器”错误。所以我在runOnUiThread()中调用了alertdialog。但是现在我无法关闭对话框。想知道这是什么原因。

@Override
public void receiveDetections(Detector.Detections<Barcode> detections){
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();
                if (barcodes.size() != 0) {
                    
                   runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {

                               
                                String val = barcodes.valueAt(0).displayValue;
                                showAlert(val);

                            } catch (Exception e) {
                                e.printStackTrace();
                           }

                        }
                    });


}

private void showAlert(String val){
        try {
            

            if (!val.equals("")) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ScannedBarcodeActivity.this);
                builder.setTitle(emp.getName())
                        .setPositiveButton(newStatus, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                emp.setStatus(newStatus);
                                viewModel.updateEmployee(emp);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();

            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

即使现在正在显示警报对话框,当我单击“取消”按钮时,警报也不会消失。

2 个答案:

答案 0 :(得分:0)

我认为receiveDetections被多次调用,这导致出现AlertDialog的多个实例。因此,当您按下 CANCEL 按钮时,您可能只取消了多个打开的对话框之一。

您可以添加日志以查看被调用receiveDetections的次数,或多次按 CANCEL 按钮,所有先前打开的警报将被取消

*可能的原因: 每次相机将图像传递到QRScanner时,都会执行 receiveDetections 。 并且当您的相机连续流传输图像时,QRScanner会一次又一次调用 receiveDetections

答案 1 :(得分:-1)

警报对话框之所以不能消除,是因为每次receiveDetections调用时,您都是AlertDialog的新对象,而在您的情况下,多次调用receiveDetections,则成为AlertDialog的新对象。

您必须创建一个Singleton类对象:

 public class singleTonDialogExample {
       static TextToSpeech t1;
       private static singleTonExample ourInstance = new singleTonDialogExample();
       private Context appContext;
       private ICallBack iCallBack;

      public interface ICallBack{
       void onYesPressed();
       void onNoPressed();
      }

       private singleTonDialogExample() { }
       public static Context get() {
          return getInstance().getContext();
       }
       public static synchronized singleTonDialogExample getInstance() {
          return ourInstance;
       }
       public void init(Context context) {
          if (appContext = = null) {
             this.appContext = context;
          }
       }
       private Context getContext() {
          return appContext;
       }

       public void setICallBack(ICallBack callBack){
         this.iCallBack=callBack;
       }
       public void AlertDialog(final MainActivity mainActivity,String title,String message) {
          AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mainActivity);
          alertDialogBuilder.setMessage(message);
          alertDialogBuilder.setPositiveButton("yes",
          new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface arg0, int arg1) {
                iCallBack.onYesPressed();
             }
          });
          alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                  iCallBack.onNoPressed();
             }
           });
           AlertDialog alertDialog = alertDialogBuilder.create();
           alertDialog.show();
        }
    } 

在Activity中创建对话框,如下所示:

  singleTonDialogExample dialog;
  dialog = singleTonExample.getInstance();
  dialog.init(getApplicationContext());
  dialog.setICallBack(this);
  dialog.AlertDialog(MainActivity.this,"Title","This is message");

还要在您的活动或片段中实现ICallBack接口。