Android Studio静音/取消静音手机 [英] Android Studio Mute/Unmute Phone

查看:346
本文介绍了Android Studio静音/取消静音手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,此链接有很大帮助: Android静音/取消静音电话
但是我并不是最擅长解释别人的代码,每次尝试在其中放入代码时,我的程序都会崩溃.有人可以帮助我解决该问题吗?花了大约一个小时试图弄清楚.

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.

推荐答案

您要做的就是在AndroidManifest.xml中添加振动许可.

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

这篇关于Android Studio静音/取消静音手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆