Android Studio Mute/Unmute Phone

匆匆过客 提交于 2019-12-23 04:29:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!