1

I'm trying to create an app which toggles the ringer volume. However when I use the volume buttons to change the ringer volume, my activity is not interrupted and thus it does not recognize the change. Is there any method which could facilitate this. The public void onWindowFocusChanged(boolean) method does not consider a pop-up as focus change,and neither do any of the regular activity OnResume() etc. methods come into play

Code:

public class MainActivity extends ActionBarActivity {
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
    CheckIfPhoneIsSilent();
    setButtonClickListener();
    toggleUi();

}


public void onWindowFocusChanged(boolean tryi)
{

    CheckIfPhoneIsSilent();
    toggleUi();
}
    private void setButtonClickListener(){
        Button toggleButton = (Button)findViewById(R.id.toggleButton);
            toggleButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (mPhoneIsSilent){
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            mPhoneIsSilent=false;
            }
            else{
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                mPhoneIsSilent=true;
            }
            toggleUi();
        }

    });
    }

    private void CheckIfPhoneIsSilent() {
        int ringerMode = mAudioManager.getRingerMode();
        if(ringerMode == AudioManager.RINGER_MODE_SILENT){
            mPhoneIsSilent= true;
        }
        else {
            mPhoneIsSilent = false;
        }
    }

    private void toggleUi()
    {
        ImageView pic = (ImageView)findViewById(R.id.phone_icon);
        Drawable newPhoneImage;
        if (mPhoneIsSilent){
            newPhoneImage = getResources().getDrawable(R.drawable.phone_silent);
        }
        else {
            newPhoneImage = getResources().getDrawable(R.drawable.phone_on);

        }
        pic.setImageDrawable(newPhoneImage);

    }


    protected void OnResume() {
        super.onResume();
        CheckIfPhoneIsSilent();
        toggleUi();
    }
Vasundhara Mehta
  • 278
  • 2
  • 3
  • 16

1 Answers1

0

Perhaps you could listen for the volume key presses within your activity? Rather than trying to anticipate if a popup for volume appears. You can do this by overriding the onKeyDown method in your activity. Then you can listen for the Key Code for volume up or down.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    boolean result = true;

    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        // Handle Volume Down
    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        // Handle Volume Up
    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
        // Handle Volume Mute
    } else {
        // Perform default behavior
        result = super.onKeyDown(keyCode, event);
    }

    // True means you handled it and it no longer needs to be bubbled up
    return result;
}
Taylor Frey
  • 374
  • 4
  • 6