某些Android应用会在设备的音量发生变化时生成通知,有些会锁定音量。对于我的生活,我不知道如何做到这一点。请有人帮我提供一个例子吗?
答案 0 :(得分:5)
没有广播操作可以检测音量变化,但您可能每隔一两次检查音量与getStreamVolume
的关系,如果需要将其锁定在特定音量,则每隔一两次使用: setStreamVolume
查看http://developer.android.com/reference/android/media/AudioManager.htm了解详情。
您可以使用AlarmManager类或处理程序每秒检查一次音量。
如果是活动,您可以覆盖onKeyDown
以检测按键操作。见http://developer.android.com/reference/android/view/View.html
答案 1 :(得分:5)
实际上,通过使用内容观察器,您可以通过一种方式进行服务。它就像一个广播接收器,听取音量,联系人,通话记录等内容变化的事件......
在您的服务中使用以下代码
public class VolumeService extends Service{
AudioManager mAudioManager;
Handler mHandler;
private ContentObserver mVolumeObserver = new ContentObserver(mHandler) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
if (mAudioManager != null) {
final int volume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
System.out.println("Volume thay đổi: " +volume);
Intent photoIntent = new Intent(VolumeService.this,TakePhotoActivity.class);
photoIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(photoIntent);
}
}
};
@Override
public void onCreate() {
super.onCreate();
System.out.println("Volume Service started");
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
Uri uri = Settings.System.getUriFor(Settings.System.VOLUME_SETTINGS[AudioManager.STREAM_RING]);
getContentResolver().registerContentObserver(uri, true, mVolumeObserver);
System.out.println("Đã đăng ký Volume listener");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Volume service destroied");
getContentResolver().unregisterContentObserver(mVolumeObserver);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
不要忘记在Android Manifest.xml中声明它
<service android:name=".service.VolumeService" >
答案 2 :(得分:0)
这是一种方法,你可以修复设定的音量而不是改变。我的目标是调整系统服务量。
另外,请避免在需要时这样做。
public class VolumeKeyController {
private MediaSessionCompat mMediaSession;
private final Context mContext;
public VolumeKeyController(Context context) {
mContext = context;
}
private void createMediaSession() {
mMediaSession = new MediaSessionCompat(mContext, KeyUtil.log);
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setPlaybackState(new Builder()
.setState(PlaybackStateCompat.STATE_PLAYING, 0, 0)
.build());
mMediaSession.setPlaybackToRemote(getVolumeProvider());
mMediaSession.setActive(true);
}
private VolumeProviderCompat getVolumeProvider() {
final AudioManager audio = mContext.getSystemService(Context.AUDIO_SERVICE);
int STREAM_TYPE = AudioManager.STREAM_MUSIC;
int currentVolume = audio.getStreamVolume(STREAM_TYPE);
int maxVolume = audio.getStreamMaxVolume(STREAM_TYPE);
final int VOLUME_UP = 1;
final int VOLUME_DOWN = -1;
return new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, maxVolume, currentVolume) {
@Override
public void onAdjustVolume(int direction) {
// Up = 1, Down = -1, Release = 0
// Replace with your action, if you don't want to adjust system volume
if (direction == VOLUME_UP) {
audio.adjustStreamVolume(STREAM_TYPE,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}
else if (direction == VOLUME_DOWN) {
audio.adjustStreamVolume(STREAM_TYPE,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}
setCurrentVolume(audio.getStreamVolume(STREAM_TYPE));
}
};
}
// Call when control needed, add a call to constructor if needed immediately
public void setActive(boolean active) {
if (mMediaSession != null) {
mMediaSession.setActive(active);
return;
}
createMediaSession();
}
// Call from Service's onDestroy method
public void destroy() {
if (mMediaSession != null) {
mMediaSession.release();
}
}
}
答案 3 :(得分:0)
您可以使用辅助功能捕获音量/硬件按键事件。