如何创建一个服务,这将是听音量按钮? [英] how to create a service, that would be listening to the volume button?

查看:96
本文介绍了如何创建一个服务,这将是听音量按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个会在后台运行,显示在通知栏图标的服务,听音量按钮是presses和改变音量prevent他们,让恼人的蜂鸣不会被听到。

你怎么想,这可能吗?如果是的,我应该在哪里开始呢?

谢谢!

编辑:

发现这个片段,为活动的伟大工程,现在必须有一个服务尝试。

  @覆盖
公共布尔dispatchKeyEvent(KeyEvent的事件){
    INT行动= event.getAction();
    INT键code = event.getKey code();
        开关(钥匙code){
        案例KeyEvent.KEY code_VOLUME_UP:
            如果(动作== KeyEvent.ACTION_UP){
                Toast.makeText(这一点,UP,1500).show();
            }
            返回true;
        案例KeyEvent.KEY code_VOLUME_DOWN:
            如果(动作== KeyEvent.ACTION_DOWN){
                Toast.makeText(这一点,DOWN,1500).show();
            }
            返回true;
        默认:
            返回super.dispatchKeyEvent(事件);
        }
    }
 

EDIT2: 找到了一个不错的教程在这里<一href="http://android.kgmoney.net/2010/05/08/creating-a-simple-android-service-for-background-processing/" rel="nofollow">http://android.kgmoney.net/2010/05/08/creating-a-simple-android-service-for-background-processing/

创建了服务,但上面的code似乎并不适合它...嗯...

解决方案
  

你怎么想,这可能吗?

没有。你可以不听的音量按钮presses的服务。

I need to create a service that would be running in the background, displaying an icon in the notification bar, listen to the volume buttons being presses and prevent them from changing the volume, so that annoying beep won't be heard.

What do you think, is that possible? And if yes, where should I start?

Thanks!

edit:

found this snippet that works great for an activity, now will have to try it with a service.

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_UP) {
                Toast.makeText(this, "UP", 1500).show();
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                Toast.makeText(this, "DOWN", 1500).show();
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }

edit2: found a nice tutorial over here http://android.kgmoney.net/2010/05/08/creating-a-simple-android-service-for-background-processing/

created a service, but the above code doesn't seem to fit into it... hmm...

解决方案

What do you think, is that possible?

No. You cannot listen for volume button presses in a service.

这篇关于如何创建一个服务,这将是听音量按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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