The Android system does not provide a broadcast of the volume buttons, and the activity's OnKeyDown method captures the volume changes only when the interface is displayed.
To get the volume key event in the lock screen or background, you can determine whether the volume key is pressed by judging the volume value change. The specific ideas for implementation are:
Open a sub-thread, continuously determine whether the current volume value and the previous volume value is consistent, if the different instructions press the volume key, and reset the volume value, so that the volume is not the maximum or minimum value.
Package Com.hy2014.phonesafer.activity;import Android.app.activity;import Android.content.context;import Android.media.audiomanager;import android.os.bundle;/** * * @author dawin Create 2015-01-19 */public class VolumeChangeA Ctivity extends activity{/** * Current volume */private int currentvolume;/** * Control Volume Object */public audiomanager maudiomanager;/** * System Maximum volume */private int maxvolume;/** * Ensure that the thread */private Boolean Isdestroy is stopped after the program is closed, @Overrideprotected void OnCreate (Bundle saved Instancestate) {super.oncreate (savedinstancestate); Isdestroy = false;//Get Audiomanager Object Maudiomanager = ( Audiomanager) Getsystemservice (context.audio_service);//music volume, if you want to listen for ringtone volume changes, change to Audiomanager.stream_ringmaxvolume = Maudiomanager.getstreammaxvolume (audiomanager.stream_music);} @Overrideprotected void OnDestroy () {Super.ondestroy (); Isdestroy = true;} /** * Monitor Volume button thread */private threads volumechangethread;/** * Continuous Monitoring volume Change Description: When the current volume changes, the volume value is reset to the maximum value minus 2 */public void Onvolumechangeli Stener () {currentvolume = Maudiomanager.getstreamvolume (AudioManager.stream_music) Volumechangethread = new Thread () {public void run () {while (!isdestroy) {int count = 0;boolean Isderease = false;//listener Interval try{thread.sleep ()} catch (Interruptedexception e) {System.out.println ("error in Onvolumechangelistener Thread.Sleep () "+ e.getmessage ());} if (Currentvolume < Maudiomanager.getstreamvolume (audiomanager.stream_music)) {Count++;currentvolume = Maudiomanager.getstreamvolume (Audiomanager.stream_music)///Set the volume equal to MaxVolume-2 because: when the volume value is the maximum and minimum, press volume plus or minus does not change, So each time it is set to a fixed value. Maudiomanager.setstreamvolume (Audiomanager.stream_music, Maxvolume-2,audiomanager.flag_remove_sound_and_vibrate );} if (Currentvolume > Maudiomanager.getstreamvolume (audiomanager.stream_music)) {Count++;currentvolume = Maudiomanager.getstreamvolume (Audiomanager.stream_music); Maudiomanager.setstreamvolume (AudioManager.STREAM_ MUSIC, maxvolume-2,audiomanager.flag_remove_sound_and_vibrate); if (count = = 1) {isderease = true;}} if (count = = 2) {System.out.println ("Press volume +");} else if (IsDErease) {System.out.println ("pressed volume-");}};}; Volumechangethread.start ();}}
Android Lock screen Status Get Volume key event