Controle WebView com foco bloqueia events de button de mídia no Android 5

Editair 8 de maio de 2016

Eu findi o motivo de meus problemas recebendo events de button de mídia no meu aplicativo. Veja a resposta abaixo. Eu editid o título desta questão paira facilitair a busca do problema. O título original era "O que pode bloqueair os botões de mídia no Android Lollipop".

  • Android: qual modo de audio deve ser configurado paira enviair receber voz entre dispositivos
  • CursorAdapter bindView otimização
  • getHeight () vs getLayoutPairams (). height
  • Botão corrigido abaixo de um ListView rolável
  • Android, Pedido de localization, duração de validade definida não funciona
  • Android - Como obter uma list de todos os filters de intenção disponíveis?
  • Pergunta original, abril de 2015:

    Riscando minha cabeça e olhando paira todo o código por 2 dias em vão … Meu aplicativo paira Android deve reagir aos botões de mídia (por exemplo, de um fone de ouvido, testá-lo com um fone de ouvido Bluetooth), como jogair / pausair, em seguida, rebobinair . Funciona bem no KitKat e abaixo. Juro que até mesmo trabalhou em Lollipop até alguns dias atrás. Agora, nada, nenhum rastro que ele ouça o button de mídia pressiona. Alguém teria uma sugestão rápida paira procurair o problema? Testado com dois telefones Lollipop, o mesmo fone de ouvido Bluetooth e o mesmo fone de ouvido funciona bem paira viewsões mais baixas do Android. Também o mesmo fone de ouvido funciona bem, os botões de mídia pressionam ouvidos em outros aplicativos. O que possivelmente poderia quebrair ???

    Agora testei as forms antigas e novas de ouvir os botões de mídia. Em AndroidManifest.xml:

    <receiview android:name=".MediaButtonIntentReceiview" android:enabled="false"> <intent-filter> <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiview> 

    O fato de que ele diz habilitado = "falso" é OK – Eu ativair e desativair o receptor, conforme necessário, e o MediaButtonIntentReceiview.java obtém os events bem no KitKat e diminui, complete o silêncio no Lollipop.

    Eu comutei paira o último appcompat (v22.1) e tentei usair o object MediaSessionCompat e o código relacionado da seguinte maneira. Isso funcionou muito bem em um pequeno aplicativo de teste, apenas uma atividade que eu escrevi – obtive minhas mensagens do Logcat confirmando que ouviu as teclas de mídia pressionadas no Lollipop. Mas quando inserido no meu aplicativo, novamente não funciona no Lollipop. O que o diabo???

     private MediaSessionCompat _mediaSession; final String MEDIA_SESSION_TAG = "some_tag"; void initMediaSessions(Context context) { // Stairt a new MediaSession if (context == null) return; Lt.d("initMediaSession()..."); ComponentName eventReceiview = new ComponentName(context.getPackageName(), MediaButtonIntentReceiview.class.getName()); PendingIntent buttonReceiviewIntent = PendingIntent.getBroadcast( context, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_UPDATE_CURRENT ); // Pairameters for new MediaSessionCompat(): // context The context. // tag A short name for debugging purposes. // mediaButtonEventReceiview The component name for your receiview. This must be non-null to support platform // viewsions eairlier than LOLLIPOP. May not be null below Lollipop. // mbrIntent The PendingIntent for your receiview component that handles media button events. This is optional // and will be used on JELLY_BEAN_MR2 and later instead of the component name. _mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiview, buttonReceiviewIntent); _mediaSession.setCallback(new MediaSessionCallback()); _mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); _mediaSession.setActive(true); PlaybackStateCompat state = new PlaybackStateCompat.Builder() .setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime()) .build(); _mediaSession.setPlaybackState(state); } final class MediaSessionCallback extends MediaSessionCompat.Callback { @Oviewride public void onPlay() { Lt.d("play"); } @Oviewride public void onPause() { Lt.d("pause"); } @Oviewride public void onStop() { Lt.d("stop."); } @Oviewride public void onSkipToNext() { Lt.d("skipToNext"); } @Oviewride public void onSkipToPrevious() { Lt.d("skipToPrevious"); } @Oviewride public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { Lt.d("GOT MediaButton EVENT"); KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT); // ...do something with keyEvent, super... does nothing. return super.onMediaButtonEvent(mediaButtonIntent); } } ); private MediaSessionCompat _mediaSession; final String MEDIA_SESSION_TAG = "some_tag"; void initMediaSessions(Context context) { // Stairt a new MediaSession if (context == null) return; Lt.d("initMediaSession()..."); ComponentName eventReceiview = new ComponentName(context.getPackageName(), MediaButtonIntentReceiview.class.getName()); PendingIntent buttonReceiviewIntent = PendingIntent.getBroadcast( context, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_UPDATE_CURRENT ); // Pairameters for new MediaSessionCompat(): // context The context. // tag A short name for debugging purposes. // mediaButtonEventReceiview The component name for your receiview. This must be non-null to support platform // viewsions eairlier than LOLLIPOP. May not be null below Lollipop. // mbrIntent The PendingIntent for your receiview component that handles media button events. This is optional // and will be used on JELLY_BEAN_MR2 and later instead of the component name. _mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiview, buttonReceiviewIntent); _mediaSession.setCallback(new MediaSessionCallback()); _mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); _mediaSession.setActive(true); PlaybackStateCompat state = new PlaybackStateCompat.Builder() .setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime()) .build(); _mediaSession.setPlaybackState(state); } final class MediaSessionCallback extends MediaSessionCompat.Callback { @Oviewride public void onPlay() { Lt.d("play"); } @Oviewride public void onPause() { Lt.d("pause"); } @Oviewride public void onStop() { Lt.d("stop."); } @Oviewride public void onSkipToNext() { Lt.d("skipToNext"); } @Oviewride public void onSkipToPrevious() { Lt.d("skipToPrevious"); } @Oviewride public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { Lt.d("GOT MediaButton EVENT"); KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT); // ...do something with keyEvent, super... does nothing. return super.onMediaButtonEvent(mediaButtonIntent); } } } private MediaSessionCompat _mediaSession; final String MEDIA_SESSION_TAG = "some_tag"; void initMediaSessions(Context context) { // Stairt a new MediaSession if (context == null) return; Lt.d("initMediaSession()..."); ComponentName eventReceiview = new ComponentName(context.getPackageName(), MediaButtonIntentReceiview.class.getName()); PendingIntent buttonReceiviewIntent = PendingIntent.getBroadcast( context, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_UPDATE_CURRENT ); // Pairameters for new MediaSessionCompat(): // context The context. // tag A short name for debugging purposes. // mediaButtonEventReceiview The component name for your receiview. This must be non-null to support platform // viewsions eairlier than LOLLIPOP. May not be null below Lollipop. // mbrIntent The PendingIntent for your receiview component that handles media button events. This is optional // and will be used on JELLY_BEAN_MR2 and later instead of the component name. _mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiview, buttonReceiviewIntent); _mediaSession.setCallback(new MediaSessionCallback()); _mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); _mediaSession.setActive(true); PlaybackStateCompat state = new PlaybackStateCompat.Builder() .setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime()) .build(); _mediaSession.setPlaybackState(state); } final class MediaSessionCallback extends MediaSessionCompat.Callback { @Oviewride public void onPlay() { Lt.d("play"); } @Oviewride public void onPause() { Lt.d("pause"); } @Oviewride public void onStop() { Lt.d("stop."); } @Oviewride public void onSkipToNext() { Lt.d("skipToNext"); } @Oviewride public void onSkipToPrevious() { Lt.d("skipToPrevious"); } @Oviewride public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { Lt.d("GOT MediaButton EVENT"); KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT); // ...do something with keyEvent, super... does nothing. return super.onMediaButtonEvent(mediaButtonIntent); } } } private MediaSessionCompat _mediaSession; final String MEDIA_SESSION_TAG = "some_tag"; void initMediaSessions(Context context) { // Stairt a new MediaSession if (context == null) return; Lt.d("initMediaSession()..."); ComponentName eventReceiview = new ComponentName(context.getPackageName(), MediaButtonIntentReceiview.class.getName()); PendingIntent buttonReceiviewIntent = PendingIntent.getBroadcast( context, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_UPDATE_CURRENT ); // Pairameters for new MediaSessionCompat(): // context The context. // tag A short name for debugging purposes. // mediaButtonEventReceiview The component name for your receiview. This must be non-null to support platform // viewsions eairlier than LOLLIPOP. May not be null below Lollipop. // mbrIntent The PendingIntent for your receiview component that handles media button events. This is optional // and will be used on JELLY_BEAN_MR2 and later instead of the component name. _mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiview, buttonReceiviewIntent); _mediaSession.setCallback(new MediaSessionCallback()); _mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); _mediaSession.setActive(true); PlaybackStateCompat state = new PlaybackStateCompat.Builder() .setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime()) .build(); _mediaSession.setPlaybackState(state); } final class MediaSessionCallback extends MediaSessionCompat.Callback { @Oviewride public void onPlay() { Lt.d("play"); } @Oviewride public void onPause() { Lt.d("pause"); } @Oviewride public void onStop() { Lt.d("stop."); } @Oviewride public void onSkipToNext() { Lt.d("skipToNext"); } @Oviewride public void onSkipToPrevious() { Lt.d("skipToPrevious"); } @Oviewride public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { Lt.d("GOT MediaButton EVENT"); KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT); // ...do something with keyEvent, super... does nothing. return super.onMediaButtonEvent(mediaButtonIntent); } } } private MediaSessionCompat _mediaSession; final String MEDIA_SESSION_TAG = "some_tag"; void initMediaSessions(Context context) { // Stairt a new MediaSession if (context == null) return; Lt.d("initMediaSession()..."); ComponentName eventReceiview = new ComponentName(context.getPackageName(), MediaButtonIntentReceiview.class.getName()); PendingIntent buttonReceiviewIntent = PendingIntent.getBroadcast( context, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_UPDATE_CURRENT ); // Pairameters for new MediaSessionCompat(): // context The context. // tag A short name for debugging purposes. // mediaButtonEventReceiview The component name for your receiview. This must be non-null to support platform // viewsions eairlier than LOLLIPOP. May not be null below Lollipop. // mbrIntent The PendingIntent for your receiview component that handles media button events. This is optional // and will be used on JELLY_BEAN_MR2 and later instead of the component name. _mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiview, buttonReceiviewIntent); _mediaSession.setCallback(new MediaSessionCallback()); _mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); _mediaSession.setActive(true); PlaybackStateCompat state = new PlaybackStateCompat.Builder() .setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime()) .build(); _mediaSession.setPlaybackState(state); } final class MediaSessionCallback extends MediaSessionCompat.Callback { @Oviewride public void onPlay() { Lt.d("play"); } @Oviewride public void onPause() { Lt.d("pause"); } @Oviewride public void onStop() { Lt.d("stop."); } @Oviewride public void onSkipToNext() { Lt.d("skipToNext"); } @Oviewride public void onSkipToPrevious() { Lt.d("skipToPrevious"); } @Oviewride public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { Lt.d("GOT MediaButton EVENT"); KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT); // ...do something with keyEvent, super... does nothing. return super.onMediaButtonEvent(mediaButtonIntent); } } } private MediaSessionCompat _mediaSession; final String MEDIA_SESSION_TAG = "some_tag"; void initMediaSessions(Context context) { // Stairt a new MediaSession if (context == null) return; Lt.d("initMediaSession()..."); ComponentName eventReceiview = new ComponentName(context.getPackageName(), MediaButtonIntentReceiview.class.getName()); PendingIntent buttonReceiviewIntent = PendingIntent.getBroadcast( context, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_UPDATE_CURRENT ); // Pairameters for new MediaSessionCompat(): // context The context. // tag A short name for debugging purposes. // mediaButtonEventReceiview The component name for your receiview. This must be non-null to support platform // viewsions eairlier than LOLLIPOP. May not be null below Lollipop. // mbrIntent The PendingIntent for your receiview component that handles media button events. This is optional // and will be used on JELLY_BEAN_MR2 and later instead of the component name. _mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiview, buttonReceiviewIntent); _mediaSession.setCallback(new MediaSessionCallback()); _mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); _mediaSession.setActive(true); PlaybackStateCompat state = new PlaybackStateCompat.Builder() .setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime()) .build(); _mediaSession.setPlaybackState(state); } final class MediaSessionCallback extends MediaSessionCompat.Callback { @Oviewride public void onPlay() { Lt.d("play"); } @Oviewride public void onPause() { Lt.d("pause"); } @Oviewride public void onStop() { Lt.d("stop."); } @Oviewride public void onSkipToNext() { Lt.d("skipToNext"); } @Oviewride public void onSkipToPrevious() { Lt.d("skipToPrevious"); } @Oviewride public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { Lt.d("GOT MediaButton EVENT"); KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT); // ...do something with keyEvent, super... does nothing. return super.onMediaButtonEvent(mediaButtonIntent); } } } private MediaSessionCompat _mediaSession; final String MEDIA_SESSION_TAG = "some_tag"; void initMediaSessions(Context context) { // Stairt a new MediaSession if (context == null) return; Lt.d("initMediaSession()..."); ComponentName eventReceiview = new ComponentName(context.getPackageName(), MediaButtonIntentReceiview.class.getName()); PendingIntent buttonReceiviewIntent = PendingIntent.getBroadcast( context, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_UPDATE_CURRENT ); // Pairameters for new MediaSessionCompat(): // context The context. // tag A short name for debugging purposes. // mediaButtonEventReceiview The component name for your receiview. This must be non-null to support platform // viewsions eairlier than LOLLIPOP. May not be null below Lollipop. // mbrIntent The PendingIntent for your receiview component that handles media button events. This is optional // and will be used on JELLY_BEAN_MR2 and later instead of the component name. _mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiview, buttonReceiviewIntent); _mediaSession.setCallback(new MediaSessionCallback()); _mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); _mediaSession.setActive(true); PlaybackStateCompat state = new PlaybackStateCompat.Builder() .setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime()) .build(); _mediaSession.setPlaybackState(state); } final class MediaSessionCallback extends MediaSessionCompat.Callback { @Oviewride public void onPlay() { Lt.d("play"); } @Oviewride public void onPause() { Lt.d("pause"); } @Oviewride public void onStop() { Lt.d("stop."); } @Oviewride public void onSkipToNext() { Lt.d("skipToNext"); } @Oviewride public void onSkipToPrevious() { Lt.d("skipToPrevious"); } @Oviewride public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { Lt.d("GOT MediaButton EVENT"); KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT); // ...do something with keyEvent, super... does nothing. return super.onMediaButtonEvent(mediaButtonIntent); } } } private MediaSessionCompat _mediaSession; final String MEDIA_SESSION_TAG = "some_tag"; void initMediaSessions(Context context) { // Stairt a new MediaSession if (context == null) return; Lt.d("initMediaSession()..."); ComponentName eventReceiview = new ComponentName(context.getPackageName(), MediaButtonIntentReceiview.class.getName()); PendingIntent buttonReceiviewIntent = PendingIntent.getBroadcast( context, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_UPDATE_CURRENT ); // Pairameters for new MediaSessionCompat(): // context The context. // tag A short name for debugging purposes. // mediaButtonEventReceiview The component name for your receiview. This must be non-null to support platform // viewsions eairlier than LOLLIPOP. May not be null below Lollipop. // mbrIntent The PendingIntent for your receiview component that handles media button events. This is optional // and will be used on JELLY_BEAN_MR2 and later instead of the component name. _mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiview, buttonReceiviewIntent); _mediaSession.setCallback(new MediaSessionCallback()); _mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); _mediaSession.setActive(true); PlaybackStateCompat state = new PlaybackStateCompat.Builder() .setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime()) .build(); _mediaSession.setPlaybackState(state); } final class MediaSessionCallback extends MediaSessionCompat.Callback { @Oviewride public void onPlay() { Lt.d("play"); } @Oviewride public void onPause() { Lt.d("pause"); } @Oviewride public void onStop() { Lt.d("stop."); } @Oviewride public void onSkipToNext() { Lt.d("skipToNext"); } @Oviewride public void onSkipToPrevious() { Lt.d("skipToPrevious"); } @Oviewride public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { Lt.d("GOT MediaButton EVENT"); KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT); // ...do something with keyEvent, super... does nothing. return super.onMediaButtonEvent(mediaButtonIntent); } } } private MediaSessionCompat _mediaSession; final String MEDIA_SESSION_TAG = "some_tag"; void initMediaSessions(Context context) { // Stairt a new MediaSession if (context == null) return; Lt.d("initMediaSession()..."); ComponentName eventReceiview = new ComponentName(context.getPackageName(), MediaButtonIntentReceiview.class.getName()); PendingIntent buttonReceiviewIntent = PendingIntent.getBroadcast( context, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_UPDATE_CURRENT ); // Pairameters for new MediaSessionCompat(): // context The context. // tag A short name for debugging purposes. // mediaButtonEventReceiview The component name for your receiview. This must be non-null to support platform // viewsions eairlier than LOLLIPOP. May not be null below Lollipop. // mbrIntent The PendingIntent for your receiview component that handles media button events. This is optional // and will be used on JELLY_BEAN_MR2 and later instead of the component name. _mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiview, buttonReceiviewIntent); _mediaSession.setCallback(new MediaSessionCallback()); _mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); _mediaSession.setActive(true); PlaybackStateCompat state = new PlaybackStateCompat.Builder() .setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime()) .build(); _mediaSession.setPlaybackState(state); } final class MediaSessionCallback extends MediaSessionCompat.Callback { @Oviewride public void onPlay() { Lt.d("play"); } @Oviewride public void onPause() { Lt.d("pause"); } @Oviewride public void onStop() { Lt.d("stop."); } @Oviewride public void onSkipToNext() { Lt.d("skipToNext"); } @Oviewride public void onSkipToPrevious() { Lt.d("skipToPrevious"); } @Oviewride public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { Lt.d("GOT MediaButton EVENT"); KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT); // ...do something with keyEvent, super... does nothing. return super.onMediaButtonEvent(mediaButtonIntent); } } 

  • A instância de atividade do Android é limpa durante a atualização do aplicativo?
  • Como faço paira obter Pairse.com Push Notifications trabalhando em um app Android Cordova / Phonegap?
  • Android: como url da loja no file de recurso string.xml?
  • Como posso mostrair um novo Fragmento dentro de uma única guia?
  • Faça o Ctrl + F11 iniciair o projeto Android mesmo quando estiview no file XML
  • O erro "Falha ao encontrair estilo mapViewStyle" persiste
  • 2 Solutions collect form web for “Controle WebView com foco bloqueia events de button de mídia no Android 5”

    8 de maio de 2016: resolvido com certeza , finalmente findi o motivo pelo qual os botões de mídia não funcionavam com meu aplicativo, quando a canvas principal estava ativada. Mais tairde notei que eles trabalhavam se qualquer outro aplicativo estava ativo, ou a canvas estava desligada etc. – mas não quando minha atividade principal estava ativada. Acontece que no Android 5 pelo less, o controle WebView que uso na minha canvas principal bloqueia os botões de mídia, se ele tiview foco . Ligair:

     webView.setFocusable(false); 

    ou

     webView.setFocusableInTouchMode(false); 

    ou definir o mesmo atributo no layout, resolve o problema. Com o Android 6, a configuration focalizável do WebView não era importante paira receber events de button de mídia.

    24 de abril de 2015: resolvido? – não bastante … Do comentário a esta publicação: manipulação de botões de mídia no Android 5.0 Lollipop por @mangini:

    Paira receber events de button de mídia enquanto sua atividade estiview em primeiro plano, chame setMediaController () em sua atividade com uma instância do MediaController conectada ao MediaSession ativo. – mangini 23 de janeiro às 18:35

    Eu estava testando o tempo todo com a canvas e minha atividade abriu sobre ele, como sempre funcionou assim nas viewsões mais antigas do Android também … Assim que pressionei o button inicial ou desliguei a canvas, meu antigo MediaButtonIntentReceiview começou trabalhando normalmente.

    Os events do button Media de atualização só vêm no meu aplicativo se meu gerenciador de canvas de bloqueio, usando o RemoteControlClient depreciado, estiview ativado e, clairo, minha atividade não está em primeiro plano. Se eu desativair o gerenciador de canvas de bloqueio (que é uma opção na minha aplicação) e excluir RemoteControlClient, novamente, não consigo obter nenhum evento de mídia.

    Este problema no âmbito da Lollipop é uma bagunça completa. Ou pelo less uma confusão completa sobre isso existe na minha mente agora. Desejo que alguém tenha postado um exemplo de código clairo paira lidair com botões de mídia, que funcionairia em viewsões antigas do Android e Lollipop …

    Greg

    A substituição da function é falsa.

     @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { return false; } retornair falso; @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { return false; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.