在处理程序后台事件中显示ProgressDialog

时间:2014-04-28 09:53:21

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

我想用文字扫描显示消息10秒钟。

通常,当我们在onPreExecute

中使用AsyncTask时,我们会执行以下操作
AlertDialog.Builder dialogo1 = 
new AlertDialog.Builder(getApplicationContext()); 
dialogo1.setMessage("Scanning...");            
dialogo1.setCancelable(false);

但是如何使用处理程序时如何做同样的事情---

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秒钟时,让我们提供一个对话框直到完成。

2 个答案:

答案 0 :(得分:0)

我想......也许

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

答案 1 :(得分:0)

你可以在这样的处理程序中使用 -

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);
相关问题