service - listen volume button when phone is locked android -


I have my service running on a backend, the service can be started when I press the button volume and close it. I pressed down the button volume.

  The public class settings expands the content observer content observer {int previousVolume; Reference Reference; Public Settings ContentAbuser (reference C, handler handler) {super (handler); Context = c; Audio Manager Audio = (Audio Manager) context.getSystemService (context.AUDIO_SERVICE); Back volume = audio. GateStream Volume (AudioManager.STREAM_RING); } @ Override Public Boolean Deliver Self-Definition () {Return Super Deliver Ascertification (); } @ Override Public Wide On Change (Boolean Self Change) {super.onChange (Swatch); Audio Manager Audio = (Audio Manager) context.getSystemService (context.AUDIO_SERVICE); Int presentVolume = audio. GateStreamVolume (audio manager. STREAM_RING); Int delta = Previous Volume-Current Volume; If (delta> 0) {System.out.println ("decrease"); Intent to intent = new intent (reference, mylarm service class); Context.stopService (intent); PreviousVolume = currentVolume; } And if (delta & lt; 0) {System.out.println ("increased"); Intent to intent = new intent (reference, mylarm service class); Context.startService (intent); PreviousVolume = currentVolume; }}}  

When my phone is locked (when the screen is turned off) then I want to do the same thing when I search the net, then I find out that I am the Broadcast Resistor I should use it test it but it does not work.

  The Public Class YourBoardcastReceiver broadcasts the receiver receipt {@Override is available on public blank (reference reference, intent to intent) {Log.e ("something gets", "I do not I know!!"); String intent action = intestine Gate action (); Key event event = null; If (Intent.ACTION_MEDIA_BUTTON.equals (intentAction)) {event = (KeyEvent) intent .getParcelableExtra (Intent.EXTRA_KEY_EVENT); } If (event == faucet) {return; } Int keycode = event.getKeyCode (); Int action = event.getAction (); Long event time = event.getEventTime (); If (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || keycode == KeyEvent.KEYCODE_HEADSETHOOK) {if (action == KeyEvent.ACTION_DOWN) {intent1 = new intent (reference, MyAlarmService.class); Context.stopService (intent1); If (Orird Bredcast) {abortBroadcast (); } And if (action == KeyEvent.ACTION_UP) {intent11 = new intent (reference, MyAlarmService.class); Context.startService (intent11); } If (Ildr breadcaste ()) {abortBroadcast (); }}}}}  

I add to the Android manifest:

  & lt; Receiver Android: name = "YourBoardcastReceiver" & gt; & Lt; Intent-Filter & gt; & Lt; Action Android: name = "android.intent.action.SCREEN_ON" /> & Lt; / Intent-Filter & gt; & Lt; / Receiver & gt;  

And in the main activity on the infusion of methode I add:

  Audio Manager mAudioManager = (Audio Manager) getSystemService (context.AUDIO_SERVICE); ComponentMemote control responder = new component name (getPackageName (), YourBoardcastReceiver.class.getName ());  

Please do I think I can get some help, I think.

When the phone is locked / sleep mode, we can not hear the button changes, You can hack something to hear the button.

Github:

They are playing media with this app 0 in the background, and then the key will always listen with the reciever with the MediaButtonIntent


Comments