我正在使用BroadcatReceiver来监控wi-fi状态变化。
我想在操作过程中显示ProgressDialog。
它显示正确但未关闭。
我的代码如下::
private final BroadcastReceiver mBluetoothChangeReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED))
{
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (state)
{
case BluetoothAdapter.STATE_OFF:
Log.i(TAG, "STATE_OFF");
showProgress(false);
// showToast("Bluetooth off");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.i(TAG, "STATE_TURNING_OFF");
showProgress(true);
// showToast("Turning Bluetooth off...");
break;
case BluetoothAdapter.STATE_ON:
Log.i(TAG, "STATE_ON");
showProgress(false);
// showToast("Bluetooth on");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.i(TAG, "STATE_TURNING_ON");
showProgress(true);
// showToast("Turning Bluetooth on...");
break;
}
}
}
};
public void showProgress(boolean show)
{
ProgressDialog mProgressDialog;
mProgressDialog = new ProgressDialog(ShortcutActivity.this);
mProgressDialog.setMessage(mContext.getString(R.string.settings_msg));
if (show)
{
mProgressDialog.setCancelable(false);
if (!mProgressDialog.isShowing())
mProgressDialog.show();
}
else
{
Log.i(TAG, " " + mProgressDialog.isShowing());
mProgressDialog.setCancelable(true);
if (mProgressDialog.isShowing())
{
mProgressDialog.dismiss();
}
}
}`
如果我的实现包含任何错误,请建议我寻求解决方案。或者我的实现方式是错误的。