Search code examples
android-audiomanagerandroid

How to disable screen lock sound in android app?


I have one android application so

  1. my application is running foreground
  2. and I click power button
  3. Beep sound comes
  4. my phone goes into sleep
  5. When i again click power button
  6. my phone wake up
  7. I unlock my phone
  8. again Beep sound comes
  9. screen goes to my application screen

Here i do not want that beep in case 3 and case 8 so how can i do that from my application code?

Edit: I know this can be done by from setting->sound->screen lock sound checkbox but i need to do this just for my app only no matter whats general setting for screen lock sound is selected.


Solution

  • Put below code in your android app where you want to turn off the screen lock sound.

    If you using Activity use below code :

    Settings.System.putInt(getContentResolver(), "lockscreen_sounds_enabled",
                                0);
    

    If you using Fragment use below code :

    Settings.System.putInt(getActivity(),getContentResolver(), "lockscreen_sounds_enabled",
                                0);
    

    To enable it again you can set 1 instead of 0.

    Thanks