2013-03-12 37 views
1

我想在沒有根的手機的情況下實現靜音效果。如何在android中實現靜音功能?

private AudioManager myAudioManager = null; 

     myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 

     int ringerMode = myAudioManager.getRingerMode();  
     myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); 

     myAudioManager.setRingerMode(ringerMode); 

我測試了上面的方法。它對一些手機有影響。而其他手機沒有任何影響。爲什麼?

除此之外,還有什麼其他靜音方式?

回答

1

可能有版本sdk問題。順便說一下,在Android中有幾個流,你想靜音哪一個?試試這個代碼:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); // mute music stream 
    audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0); // mute ring stream 
    if (Build.VERSION.SDK_INT >= 8) { 
     audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); 
     audioManager.requestAudioFocus(null, AudioManager.STREAM_RING, AudioManager.AUDIOFOCUS_GAIN); 
    }