处理Android上的锁屏的体积变化? [英] Handling volume change on android lockscreen?

查看:228
本文介绍了处理Android上的锁屏的体积变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是,能够赶上音量锁屏上/下按钮动作在Android 4.4。

What I am trying to do is, being able to catch volume up/down button actions on lockscreen on android 4.4.

谷歌演员设计清单文档描述了锁屏的要求提供访问通过硬件按钮,音量控制。我尝试了各种方法来处理锁定屏幕上的硬件音量按钮,但没有一次成功。

Google Cast Design Checklist document describes lock screen requirement "Provide access to the volume control via hardware buttons". I tried various ways to handle hardware volume buttons on lock screen but none of them worked.


  1. 的onkeydown / dispatchKeyEvent - 我试图重写的onkeydown以及对活动dispatchKeyEvent方法,但这些都不是在锁屏,这些只是工作时,我的应用程序的重点是执行

  1. onKeyDown/dispatchKeyEvent - I tried to override onKeyDown as well as dispatchKeyEvent methods on Activity, but none of these are executed on lockscreen, these only works when my app is focused.

Settings.System.CONTENT_URI / ContentObserver - 上注册的主要活动内容解析器内容观察者不赶系统设置改变,但也不会在锁定画面出现

Settings.System.CONTENT_URI/ContentObserver - Registering content observer on main activity's content resolver does catch the system settings change, but that also does not occur on lockscreen.

android.intent.action.MEDIA_BUTTON - 具有明显的此过滤器,我能够接受的播放/暂停从锁屏动作然而没有体积变化事件

android.intent.action.MEDIA_BUTTON - Having this filter in manifest, I am able to receive play/pause actions from lockscreen however no volume change event.

android.media.VOLUME_CHANGED_ACTION - 有清单中该滤波器,在接收我的BroadcastReceiver这种情况下,不幸的是它的附加价值永远不会在锁定画面改变。当我不停的按音量键,返回android.media.EXTRA_VOLUME_STREAM_VALUE保持不变(即始终为1),即使是我的广播接收器接收更改的动作。

android.media.VOLUME_CHANGED_ACTION - Having this filter in manifest, this event is received in my BroadcastReceiver, unfortunately its extra values never get changed on lockscreen. When I keep hitting the volume button, the returned android.media.EXTRA_VOLUME_STREAM_VALUE remains the same (i.e. always 1), even though the CHANGED action is received by my broadcast receiver.

CastVideos-的Andr​​oid - 参考Android的发件人应用程序似乎能够从发件人controling即使以控制接收器音量锁屏,但是,即使把breakpoitns遍布各地演员的地方后, .CastApi.setVolume(),这将不会得到回升。如此看来,命令正在发送到接收器从什么地方我找不到。

CastVideos-android - The reference android sender app seems to be able to control volume on receiver even when controling from senders lock screen, however even after putting breakpoitns all over the place around Cast.CastApi.setVolume(), these would not get picked. So it seems that the command is being send to a receiver from somewhere I can not locate.

我还可以看到一些其他的应用程序能够赶上硬件音量键即的播放音乐的应用程序。所以,我的设备肯定能够...

I can also see some other apps being able to catch HW volume keys i.e. Play Music app. So my device surely is capable...

任何人都可以提出任何解决方案的工作?

Can anyone suggest any working solution?

推荐答案

在最后它似乎我缺少code的一行:

At the end it appeared I was missing one line of code:

MediaRouter.getInstance(context).addRemoteControlClient(remoteControlClient);

MediaRouter.getInstance(activity).setMediaSession(session.getMediaSession());

下面是从我的执行4.0一些code +:

here is some more code from my implementation for 4.0+:

import android.media.AudioManager;
import android.media.RemoteControlClient;
import android.support.v7.media.MediaRouter;

remoteControlClient = new RemoteControlClient(pendingIntent);
remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);

audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.registerMediaButtonEventReceiver(receiver);
audioManager.registerRemoteControlClient(remoteControlClient);

MediaRouter.getInstance(context).addRemoteControlClient(remoteControlClient);

和5.0 +

import android.media.AudioManager;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.media.MediaRouter;

session = new MediaSessionCompat(activity, TAG);
session.setPlaybackToLocal(AudioManager.STREAM_MUSIC);

MediaRouter.getInstance(activity).setMediaSession(session.getMediaSession());

有趣的是,有一些黑魔法在内部控制听筒音量和你自己的 Cast.CastApi.setVolume()呼叫没有在所有参与

这篇关于处理Android上的锁屏的体积变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆