0

我正在使用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();
        }
    }
}`

如果我的实现包含任何错误,请建议我寻求解决方案。或者我的实现方式是错误的。

4

4 回答 4

1
  1. 将以下内容设为全局变量

    ProgressDialog mProgressDialog;
    
  2. 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();
    }
}}
于 2015-09-15T06:26:16.580 回答
1

在您的 showProgress 方法中,每次调用它时都会创建一个新的 Progressdialog。您必须关闭现有的 ProgressDialog(当前显示的那个),而不是新创建的。

没错。</p>

于 2015-09-15T06:32:48.410 回答
0

您需要在“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();
        }
    }
}
于 2015-09-15T06:17:04.663 回答
0

ProgressDialog mProgressDialog;在外面创建并启动onCreate()

mProgressDialog = new ProgressDialog(ShortcutActivity.this);. 现在它工作正常。

谢谢你的建议

于 2015-09-15T06:14:12.380 回答