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);