接听来电时振动设置未关闭 - Android
我想在来电时关闭设备振动设置。我已经为此功能实现了一个 BroadcastReceiver,它执行接收 PHONE_STATE
广播的操作。问题是我根本无法关闭振动。我尝试过以下方法:
AudioManager audioManager = (AudioManager)
context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
AudioManager.VIBRATE_SETTING_OFF);
或
Vibrator vib = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vib.cancel();
第一种方法对我来说似乎很理想。它甚至可以在接到电话时“打开”振动。但是,在这种情况下我无法将它们“关闭”。
有人尝试过这个吗?
编辑:我检查了 Android 的电话应用程序代码。以下代码位于 Ringer.java< /a>:
if (shouldVibrate() && mVibratorThread == null) {
mContinueVibrating = true;
mVibratorThread = new VibratorThread();
mVibratorThread.start();
}
线程最初由手机应用程序启动,该应用程序会振动手机。当我将振动设置更改为关闭时,会跳过此检查,但已启动的线程继续运行。
这也解释了如何在来电时打开振动。在这种情况下,线程最初不会运行。然后,当我打开振动设置时它就会启动。我认为如果不更改电话应用程序就没有任何解决方案。
I want to turn off device vibrate setting when a call comes. I have implemented a BroadcastReceiver for this feature which performs the action on receiving PHONE_STATE
broadcast. The problem is that I am not able to turn off vibrations at all. I have tried the following:
AudioManager audioManager = (AudioManager)
context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
AudioManager.VIBRATE_SETTING_OFF);
or
Vibrator vib = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vib.cancel();
The first approach seems ideal to me. It even works for turning "on" the vibrations when a call is received. But, I am not able turn them "off" in this scenario.
Has anybody tried this?
Edit: I checked Android's Phone app code. Following code is present in Ringer.java:
if (shouldVibrate() && mVibratorThread == null) {
mContinueVibrating = true;
mVibratorThread = new VibratorThread();
mVibratorThread.start();
}
A thread is started initially by Phone app which vibrates the phone. When I change the vibrate setting to off this check is skipped but the already started thread keeps on running.
This also explains how vibrations can be turned on when an incoming call comes. In that case, the thread is not running initially. Then, it is started when I turn on the vibrate setting. I don't think there is any solution to the problem without changing the Phone app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
this:
不会对您有帮助,因为它会取消振动器的新实例。
我可以想象,如果已经有来电,因为振动已经在进行中,那么关闭设备的振动为时已晚。所以你必须在电话打进来之前这样做。
this:
is not going to help you as it cancels the new instance of the Vibrator.
I can imagine that it's too late to turn of the vibration of the device if the call is already incoming as the vibration is already in progress. So you'd have to do that before the call comes in.