Android系统没有提供音量按键的广播,而Activity的onKeyDown方法只有在界面显示时才能捕获音量变化,
要在锁屏状态或后台获得音量按键事件,可以通过判断音量值的改变来判断是否按下了音量键。实现的具体思路是:
开一个子线程,持续判断当前音量值和之前音量值是否一致,如果不同说明按下了音量键,并且重置音量值,使音量不在最大值或最小值上。
package com.hy2014.phonesafer.activity;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
/**
*
* @author Dawin Create 2015-01-19
*/
public class VolumeChangeActivity extends Activity
{
/**
* 当前音量
*/
private int currentVolume;
/**
* 控制音量的对象
*/
public AudioManager mAudioManager;
/**
* 系统最大音量
*/
private int maxVolume;
/**
* 确保关闭程序后,停止线程
*/
private boolean isDestroy;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
isDestroy = false;
// 获得AudioManager对象
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);//音乐音量,如果要监听铃声音量变化,则改为AudioManager.STREAM_RING
maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}
@Override
protected void onDestroy()
{
super.onDestroy();
isDestroy = true;
}
/**
* 监听音量按键的线程
*/
private Thread volumeChangeThread;
/**
* 持续监听音量变化 说明: 当前音量改变时,将音量值重置为最大值减2
*/
public void onVolumeChangeListener()
{
currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
volumeChangeThread = new Thread()
{
public void run()
{
while (!isDestroy)
{
int count = 0;
boolean isDerease = false;
// 监听的时间间隔
try
{
Thread.sleep(20);
} catch (InterruptedException e)
{
System.out.println("error in onVolumeChangeListener Thread.sleep(20) " + e.getMessage());
}
if (currentVolume < mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC))
{
count++;
currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
// 设置音量等于 maxVolume-2的原因是:当音量值是最大值和最小值时,按音量加或减没有改变,所以每次都设置为固定的值。
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume - 2,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}
if (currentVolume > mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC))
{
count++;
currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume - 2,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
if (count == 1)
{
isDerease = true;
}
}
if (count == 2)
{
System.out.println("按下了音量+");
} else if (isDerease)
{
System.out.println("按下了音量-");
}
}
};
};
volumeChangeThread.start();
}
}