Escuchar el botón de volumen cuando el teléfono está bloqueado android

Tengo un servicio en mi aplicación que se ejecutan en backend, el servicio se puede iniciar cuando presione el botón de volumen hacia arriba y se detuvo cuando presione el botón de volumen hacia abajo.

public class SettingsContentObserver extends ContentObserver { int previousVolume; Context context; public SettingsContentObserver(Context c, Handler handler) { super(handler); context=c; AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); previousVolume = audio.getStreamVolume(AudioManager.STREAM_RING); } @Override public boolean deliverSelfNotifications() { return super.deliverSelfNotifications(); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING); int delta=previousVolume-currentVolume; if(delta > 0) { System.out.println("Decreased") ; Intent intent = new Intent(context, MyAlarmService.class); context.stopService(intent); previousVolume=currentVolume; } else if(delta < 0) { System.out.println("Increased"); Intent intent = new Intent(context, MyAlarmService.class); context.startService(intent); previousVolume=currentVolume; } } } 

Quiero hacer lo mismo cuando mi teléfono está bloqueado (cuando la pantalla está apagada), cuando busco en la red encuentro que debo usar un BroadcastReceivre, lo pruebo pero no funciona.

 public class YourBoardcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.e("get something", "i dont know what!!"); String intentAction = intent.getAction(); KeyEvent event = null; if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { event = (KeyEvent) intent .getParcelableExtra(Intent.EXTRA_KEY_EVENT); } if (event == null) { return; } int keycode = event.getKeyCode(); int action = event.getAction(); long eventtime = event.getEventTime(); if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || keycode == KeyEvent.KEYCODE_HEADSETHOOK) { if (action == KeyEvent.ACTION_DOWN) { Intent intent1 = new Intent(context, MyAlarmService.class); context.stopService(intent1); if (isOrderedBroadcast()) { abortBroadcast(); } else if (action == KeyEvent.ACTION_UP) { Intent intent11 = new Intent(context, MyAlarmService.class); context.startService(intent11); } if (isOrderedBroadcast()) { abortBroadcast(); } } } } } 

En el manifiesto android agrego:

 <receiver android:name="YourBoardcastReceiver"> <intent-filter> <action android:name="android.intent.action.SCREEN_ON" /> </intent-filter> </receiver> 

Y en el mainActivity en el método oncrete agrego:

 AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ComponentName mRemoteControlResponder = new ComponentName(getPackageName(), YourBoardcastReceiver.class.getName()); 

Puedo darme algo de ayuda, piensa.

Cuando el teléfono está bloqueado / modo de sueño no podemos escuchar los cambios de botón, Sin embargo, podemos hacer algunos hack para escuchar el botón de volumen.

Github: https://github.com/praslnx8/volume_Screen_Lock

Esas aplicaciones están reproduciendo medios con 0 volumen en segundo plano y la tecla siempre será escuchada por el receptor con MediaButtonIntent

Intente Registrarse con BroadCast Receiver para Intent.ACTION_SCREEN_OFF y Intent.ACTION_SCREEN_ON

  • Cómo aumentar el nivel de volumen de micrófono en Android PJSIP?
  • Cómo ajustar el volumen en openSL ES (Android)?
  • Cómo controlar el volumen de audio que se está reproduciendo desde el servicio?
  • No se puede establecer el volumen en BlackBerry Playbook
  • Cómo convertir el volumen a max programmatically en android?
  • OpenSL ES en Android con SL_ANDROID_STREAM_VOICE. La entrada de micrófono es extremadamente baja
  • Controlar el volumen en MediaPlayer
  • ¿Cómo puedo bajar el volumen de MediaPlayer cuando los auriculares están conectados?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.