Sruit A.Suk
Sruit A.Suk

Reputation: 7273

disable android 'default sound' from hardware 'back' button

Ok, if we want to disable android 'default sound' from hardware 'back' button

if it is object in android

We can disable it by

onClick (View v) {
    v.setSoundEffectsEnabled(false); 
}

or disable it through xml by

<Button
    android:id="@+id/bSignin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:soundEffectsEnabled="false" 
/>

however, if it is hardware button

how to do that ?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK )
    {
    // ?
    }
}

NOTE: i don't want to completely disable all sound, i just want to disable default sound and play my application sound instead

Upvotes: 2

Views: 1397

Answers (3)

aaroncarsonart
aaroncarsonart

Reputation: 1114

This is possible, I found this answer and wanted to share it, as I was able to disable the hardware default back button with this solution. Here's the code:

@override
public void onResume(){
    super.onResume();
    AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
}


@override
public void onPause(){
    super.onPause();
    AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false);
}

Upvotes: 3

user3467178
user3467178

Reputation: 83

You can try this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK )
    {
        return false;
    }
}

Upvotes: 0

tasomaniac
tasomaniac

Reputation: 10342

I don't think it is possible.

There is an option in the System Settings for the user to do that. The hardware back button is not the part of your application. It is user's job to decide if they want to hear it or not. You cannot decide that.

Upvotes: -1

Related Questions