Android Lock screen Status Get Volume key event

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.