问题
So this link helps a lot: Android mute/unmute phone
But I am a not the best at interpreting other people's code and everytime I try to put in there code my program crashes. Could someone help me with how to I can fix it? Spent about an hour trying to figure it out.
回答1:
All you have to do is in your AndroidManifest.xml add the permission for vibration.
<uses-permission android:name="android.permission.VIBRATE" />
After that in your activity whenever you want to change it to vibrate use this code (button press etc.)
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
To unmute simply use the code
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
来源:https://stackoverflow.com/questions/28801646/android-studio-mute-unmute-phone