【问题标题】:Show ProgressDialog while in handler background event在处理程序后台事件中显示 ProgressDialog
【发布时间】:2014-06-13 19:27:27
【问题描述】:

我想显示带有文本 Scanning for 10 seconds 的消息。

当我们在AsyncTask 中使用onPreExecute 时,通常我们会执行以下操作,

AlertDialog.Builder dialogo1 = 
new AlertDialog.Builder(getApplicationContext()); 
dialogo1.setMessage("Scanning...");            
dialogo1.setCancelable(false);

但是在使用 handler 时如何做同样的事情,如下所示---

private static final long SCAN_PERIOD = 10000;
private void scanLeDevice(final boolean enable) {
if (enable) {
    // Stops scanning after a pre-defined scan period.
    mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        mScanning = false;
        Log.d(TAG,getCtx() + "run stopLeScan");
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }, SCAN_PERIOD);
    Log.d(TAG,getCtx()+" scanLeDevice startLeScan:"+enable);
    mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
    Log.d(TAG,getCtx()+ " scanLeDevice stopLeScan:"+enable);
    mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
}

所以当扫描在后台处理 10 秒时,让我们提供一个对话框直到它完成。

【问题讨论】:

  • 你想达到什么目的?
  • 我想要时间处理程序的警报对话框正在对 LE 设备执行扫描,即 SCAN_PERIOD 给出的时间为 10000 毫秒。我们如何在 handler 中使用上述方法?

标签: android android-alertdialog android-progressbar android-handler


【解决方案1】:

您可以像这样在处理程序中使用--

ProgressDialog pDialog = new ProgressDialog(Context);
    pDialog.setTitle("Please Wait!");
    pDialog.setMessage("Scanning...");
    pDialog.show();

new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            pDialog.dismiss();
        }
    }, SCAN_PERIOD);

【讨论】:

    【解决方案2】:

    我认为....也许

    if (enable) {
         //show dialog box here
    mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        mScanning = false;
        //dismiss dialog box here
        Log.d(TAG,getCtx() + "run stopLeScan");
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }, SCAN_PERIOD);
    Log.d(TAG,getCtx()+" scanLeDevice startLeScan:"+enable);
    mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
    Log.d(TAG,getCtx()+ " scanLeDevice stopLeScan:"+enable);
    mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 2017-01-26
      • 2014-12-06
      • 1970-01-01
      相关资源
      最近更新 更多