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-method では、呼び出すたびに新しい 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 に答える