Search code examples
androidprogressdialogandroid-wifiandroid-broadcastandroid-broadcastreceiver

ProgressDialog is not closing with BroadcastReceiver


I am using BroadcatReceiver to monitor wi-fi state changes.

And I want to show ProgressDialog during the operation.

It is showing properly but not closing.

My code is like below::

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();
        }
    }
}`

Please suggest me for solution if my implementation contains any thing wrong. Or my way of implementation was wrong.


Solution

    1. make the below as global variable

      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();
        }
    }}