当点击按钮,连接wifi时,在android

时间:2016-03-21 06:45:11

标签: android

最近,当点击按钮WI-FI连接时。

但我想在连接期间显示进度对话框

我该怎么办?

protected final ScanResult mScanResult;
 private OnClickListener mConnectOnClick = new OnClickListener() {
 @Override
    public void onClick(View v) {
 final WifiConfiguration config = Wifi.getWifiConfiguration(mWifiManager, mScanResult, mScanResultSecurity);
        boolean connResult = false;

if (config != null) {
       connResult = Wifi.connectToConfiguredNetwork(mFloating, mWifiManager, config, false);
    // I Think this part progress dialog. 
 }
        if (!connResult) {
            Toast.makeText(mFloating, R.string.toastFailed, Toast.LENGTH_LONG).show();
        }

        mFloating.finish();

如果完成连接wifi, 我想停止进度对话框
感谢。

1 个答案:

答案 0 :(得分:0)

为此,您可以使用异步任务

 class WIFIConfigurationTask extends AsyncTask<String, Void, Boolean> {
            ProgressDialog dialog;
           protected final ScanResult mScanResult;



            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog = new ProgressDialog(Your_Activity.this);
                dialog.setCancelable(false);
                dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
                dialog.setMessage(Constant.KEY_PLEASE_WAIT);
                dialog.show();
            }




            @Override
            protected Boolean doInBackground(String... params) {
               //Background Task
            }

            @Override
            protected void onPostExecute(Boolean response) {
                try {
                    super.onPostExecute(response);
                    if (isCancelled())
                        return;

                    dialog.dismiss();
     private OnClickListener mConnectOnClick = new OnClickListener() {
 @Override
    public void onClick(View v) {
 final WifiConfiguration config = Wifi.getWifiConfiguration(mWifiManager, mScanResult, mScanResultSecurity);
        boolean connResult = false;

if (config != null) {
       connResult = Wifi.connectToConfiguredNetwork(mFloating, mWifiManager, config, false);
    // I Think this part progress dialog. 
 }
        if (!connResult) {
            Toast.makeText(mFloating, R.string.toastFailed, Toast.LENGTH_LONG).show();
        }

        mFloating.finish();
                   }
        }

可能有效。(未经测试)