ProgressDialog 未使用 BroadcastReceiver 关闭
ProgressDialog is not closing with BroadcastReceiver
我正在使用 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();
}
}
}`
如果我的实现有任何错误,请建议我寻求解决方案。
或者我的实现方式不对。
我创造了
ProgressDialog mProgressDialog;
在外面并在 onCreate()
中启动它作为
mProgressDialog = new ProgressDialog(ShortcutActivity.this);
。现在一切正常。
感谢您的建议
您需要在 "showProgress" 方法外和 showProgress 方法内声明 mProgressDialog,如果为 null 则初始化 mProgressDialog
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();
}
}
}
将以下内容设为全局变量
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();
}
}}
在您的 showProgress 方法中,每次调用它时都会创建一个新的 Progressdialog。您必须关闭现有的 ProgressDialog(当前显示的那个),而不是新创建的。
没错。
我正在使用 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();
}
}
}`
如果我的实现有任何错误,请建议我寻求解决方案。 或者我的实现方式不对。
我创造了
ProgressDialog mProgressDialog;
在外面并在 onCreate()
中启动它作为
mProgressDialog = new ProgressDialog(ShortcutActivity.this);
。现在一切正常。
感谢您的建议
您需要在 "showProgress" 方法外和 showProgress 方法内声明 mProgressDialog,如果为 null 则初始化 mProgressDialog
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();
}
}
}
将以下内容设为全局变量
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();
}
}}
在您的 showProgress 方法中,每次调用它时都会创建一个新的 Progressdialog。您必须关闭现有的 ProgressDialog(当前显示的那个),而不是新创建的。
没错。