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.
- RegisterMediaButtonEventReceiver no recibe eventos
- En Android (utilizando Java), ¿cómo puedo configurar el volumen del timbre (a un número especificado)?
- ¿Cómo habilito el altavoz de Android durante una llamada, desde el código?
- Control del volumen de Exoplayer con una barra de búsqueda
- Android: ¿Detectar el estado de No molestar?
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.
- Cambiar el volumen del timbre durante el timbre
- Usando SeekBar para controlar el volumen en android?
- Volumen multimedia en lugar de Volumen del teléfono en Android
- El volumen del altavoz (Alarma) disminuye cuando se conectan los auriculares
- Deshabilitar el volumen tostado
- Android BroadCastReceiver para la tecla de volumen arriba y abajo
- Auricular Auricular Bluetooth
- Ocultar UI cuando se pulsa el volumen hacia arriba en un diálogo
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
- El estudio de Android evita que el código se vuelva a formatear en la pasta
- YouTube API 3 Subir video – Acceso no configurado – Android