不断为您在Android的服务量变化 [英] Constantly check for volume change in Android services

查看:142
本文介绍了不断为您在Android的服务量变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这片code,这显然是有缺陷的。我该如何去创造它会不断检查成交量的变化服务?关键听众不能在服务中使用,请不要发表与音量键听众的答案。

我的code是错误的,因为我已经给假人条件while循环。有什么是条件,我的服务,以检查体积变化并不会崩溃?它不能是 isScreenOn(),因为量能边听音乐,屏幕关闭时被改变。

code

  AudioManager音频=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
    INT currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
    INT newVolume = 0;
    而(1 == 1)
    {
        newVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
        如果(currentVolume!= newVolume)
        {
            Toast.makeText(这一点,音量变化来检测!,Toast.LENGTH_LONG).show();
        }

    }
 

解决方案

如果你想看到的时候设置的变化,你可以注册一个ContentObserver的设置提供对其进行监视。例如,这是在设置应用程序的观察员:

 私人ContentObserver mVolumeObserver =新ContentObserver(mHandler){
        @覆盖
        公共无效的onChange(布尔selfChange){
            super.onChange(selfChange);
            如果(mSeekBar = NULL和放大器;!&安培;!mAudioManager = NULL){
                INT体积= mAudioManager.getStreamVolume(mStreamType);
                mSeekBar.setProgress(体积);
            }
        }
    };
 

和它注册到观察者的设置是这样的:

 进口android.provider.Settings;
        进口android.provider.Settings.System;

        mContext.getContentResolver()。registerContentObserver(
                System.getUriFor(System.VOLUME_SETTINGS [mStreamType]),
                假的,mVolumeObserver);
 

I wrote this piece of code, it's obviously flawed. How do I go about creating a service which will constantly check for changes in volume? Key listeners cannot be used in services, please don't post an answer with volume key listeners.

My code is wrong because I've given dummy condition for the while loop. What has to be the condition for my service to check for volume change and not crash? It can't be isScreenOn() because volume can be changed while listening to music and the screen is off.

CODE

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
    int newVolume=0;
    while(1==1)
    {
        newVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
        if(currentVolume!=newVolume)
        {
            Toast.makeText(this, "Volume change detected!", Toast.LENGTH_LONG).show();
        }

    }

解决方案

If you want to see when the setting changes, you can register a ContentObserver with the settings provider to monitor it. For example, this is the observer in the settings app:

    private ContentObserver mVolumeObserver = new ContentObserver(mHandler) {
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            if (mSeekBar != null && mAudioManager != null) {
                int volume = mAudioManager.getStreamVolume(mStreamType);
                mSeekBar.setProgress(volume);
            }
        }
    };

And it is registered to observer the settings like this:

        import android.provider.Settings;
        import android.provider.Settings.System;

        mContext.getContentResolver().registerContentObserver(
                System.getUriFor(System.VOLUME_SETTINGS[mStreamType]),
                false, mVolumeObserver);

这篇关于不断为您在Android的服务量变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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