有什么方法可以通过android服务检测音量按键或音量变化? [英] any way to detect volume key presses or volume changes with android service?

查看:18
本文介绍了有什么方法可以通过android服务检测音量按键或音量变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些 Android 应用会在设备音量发生变化时生成通知,而某些应用会锁定音量.对于我的生活,我不知道这是怎么做到的.拜托,有人能帮我举个例子吗?

Some Android apps generate a notification when the device's volume changes and some lock the volume. For the life of me, I cannot find out how that's done. Please, can someone help me by providing an example?

推荐答案

没有广播动作来检测音量变化,但你可以每隔一两秒用 getStreamVolume 和如果您需要将其锁定在特定音量,每隔一两次使用:setStreamVolume

There is no Broadcast Action to detect volume changes, but you could maybe every second or two check what the volume is with getStreamVolume and if you need to lock it at a specific volume, every second or two use: setStreamVolume

检查 http://developer.android.com/reference/android/media/AudioManager.htm 了解更多信息.

Check http://developer.android.com/reference/android/media/AudioManager.htm for more info.

您可以使用 AlarmManager 类或处理程序每​​隔一秒左右检查一次音量.

You could use the AlarmManager class or a handler to check the volume every second or so.

如果是活动,您可以覆盖 onKeyDown 以检测按键.请参阅 http://developer.android.com/reference/android/view/View.html

If it's an activity, you can override onKeyDown to detect key presses. See http://developer.android.com/reference/android/view/View.html

这篇关于有什么方法可以通过android服务检测音量按键或音量变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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