我正在使用 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();
}
}
}`
如果我的实现包含任何错误,请建议我解决。 或者我的实施方式是错误的。
答案 0 :(得分:1)
将以下内容设为全局变量
ProgressDialog mProgressDialog;
的OnCreate()
mProgressDialog = new ProgressDialog(ShortcutActivity.this);
mProgressDialog.setMessage(mContext.getString(R.string.settings_msg));
3
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;
}
}
}
};
4
public void showProgress(boolean show)
{
if (show)
{
mProgressDialog.setCancelable(false);
if (!mProgressDialog.isShowing())
mProgressDialog.show();
}
else
{
Log.i(TAG, " " + mProgressDialog.isShowing());
mProgressDialog.setCancelable(true);
if (mProgressDialog.isShowing())
{
mProgressDialog.dismiss();
}
}}
答案 1 :(得分:1)
在showProgress方法中,每次调用时都会创建一个新的Progressdialog。您必须关闭现有的ProgressDialog(当前显示的那个),而不是新创建的。
那是对的。
答案 2 :(得分:0)
我创造了
ProgressDialog mProgressDialog;
位于外部,并在onCreate()
中以
mProgressDialog = new ProgressDialog(ShortcutActivity.this);
。现在它工作正常。
感谢您的建议
答案 3 :(得分:0)
您需要在" showProgress"之外声明mProgressDialog。方法和内部showProgress方法,初始化mProgressDialog,如果它为null
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;
}
}
}
};
ProgressDialog mProgressDialog;
public void showProgress(boolean show)
{
if (mProgressDialog == null)
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();
}
}
}