自动沉默Android手机的音量编程?音量、沉默、手机、Android

2023-09-13 00:57:13 作者:等XX长大

我正在开发能自动关闭Android手机的声音的应用程序。我怎样才能检测到声音的音量,并关闭它编程?

I'm developing an application that can turn off the sound of Android Phone automatically. How can I detect the volume of the sound and turn it off programmatically?

    if (hour == myTime.getHour() && minute == myTime.getMinute()) {
        if (Settings.getSetMyTime(this))
            showNotificationAlarm(R.drawable.icon,
                    "It's time to work");
                    ///so, i want to add the silet function here..help me, please?
    }

在此先感谢。

Thanks in advance.

推荐答案

注册 AUDIO_SERVICE 然后使用 AudioManager 控制音量加/减或设置配置文件。

Register for AUDIO_SERVICE and then use the AudioManager to control the volume up/down or set profiles.

或者,如果你要听的变化将音频,然后让你的活动实现AudioManager.OnAudioFocusChangeListener 。重写未实现的方法。创建开关,将利用类型的变化的照顾。

Or if you want to listen for changes in the Audio focus then make your Activity implements AudioManager.OnAudioFocusChangeListener. Override unimplemented method. Create switch that will take care of types of changes.

像:

@Override
    public void onAudioFocusChange(int focusChange) {
        switch(focusChange)
        {
        case AudioManager.AUDIOFOCUS_GAIN:
            //do something
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            break;
        }

或者,如果你想监听的音频输出变化,就像拔掉耳机(切换到手机扬声器)使用 ACTION_AUDIO_BECOMING_NOISY 持久广播

http://developer.android.com/reference/android/media/AudioManager.html#ACTION_AUDIO_BECOMING_NOISY

的http://developer.android.com/reference/android/media/AudioManager.html#RINGER_MODE_SILENT

在这里看到

修改:这是解决方案。有没有需要处理AudioFocus只是设置不同的铃声配置文件或调整音量

Edit: This is the solution. There was no need to handle AudioFocus but just set different ringer profile or adjusting volume

if (hour == myTime.getHour() && minute == myTime.getMinute()) {
        if (Settings.getSetMyTime(this))
            showNotificationAlarm(R.drawable.icon,
                    "It's time to work");
                    AudioManager audiomanager =(AudioManager)YourActivityName.this.getSystemService(Context.AUDIO_SERVICE);
        audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT); //or adjust volume here instead setting silent profile for the ringer
    }