码迷,mamicode.com
首页 > 移动开发 > 详细

Android 音量键的监听

时间:2015-01-05 11:11:25      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

实践后的结论: 

下面两种都可以获取到音量键的东西,  测试的时候,发现  

KeyEvent.KEYCODE_VOLUME_UP 一直按住的时候, 三星的一款平板计数到200,就不在触发该事件;

所以改为 KeyEvent.ACTION_DOWN 时启动一个方法用来计数, KeyEvent.ACTION_UP的停止技数;  

package com.akm.testvolume;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
 
public class MainActivity extends Activity {
    private TextView tv;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
    }
 
    int count = -1;
 
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
 
        int action = event.getAction();
 
        if (action ==KeyEvent.ACTION_DOWN) {
            tv.setText("+++++++++ACTION_DOWN++++++"+ count++);
            return true;
        }
 
        if (action== KeyEvent.ACTION_UP) {
            tv.setText("+++++ACTION_UP++++++++++");
            return true;
        }
 
        return super.dispatchKeyEvent(event);
    }
 
 
 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
 
        case KeyEvent.KEYCODE_VOLUME_DOWN:
 
            tv.setText("-----------------"+count);
            count--;
 
            return true;
 
        case KeyEvent.KEYCODE_VOLUME_UP:
            tv.setText("++++++++++++++++"+ count);
            count++;
            return true;
        case KeyEvent.KEYCODE_VOLUME_MUTE:
            tv.setText("MUTE");
 
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
 
}


Android 音量键的监听

标签:

原文地址:http://blog.csdn.net/aikongmeng/article/details/42419527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!