如何针对特定来电提供定制振动

发布于 2024-12-25 18:20:03 字数 640 浏览 2 评论 0原文

该程序的功能如下:用户有一个电话号码列表,只有当没有其他系统范围的应用程序提供振动(例如在静音模式下)时,手机才会在来电时振动。我知道这在某种程度上违反了规则,因为应用程序应该尊重用户的设置,但应用程序仅限于某些有此需求的用户。我尝试了两种方法,但都不满意:

  1. 监听电话状态并用我自己的模式直接触发振动服务(使用Vibrator.vibrate())。此方法在没有来电的情况下有效,但在手机处于 CALL_STATE_RINGING 状态时随机有效,我猜这是因为与实际处理来电振动的系统范围应用程序发生冲突。

  2. 判断手机来电时是否振动(使用AudioManager.shouldVibrate()),并决定是否更改振动设置(使用AudioManager.setRingerMode() > 和 AudioManager.setVibrateSetting())。如果我的应用程序更改了振动设置,则一旦手机返回到 CALL_STATE_IDLE 状态,这些设置就会恢复。但这个方法有时还是不行,没有任何原因。

我希望有人可以就这个问题提供一些建议。欢迎对这两种方式发表评论或其他建议。

The program functions like this: the user has a list of phone numbers, for which the cellphone could vibrate upon an incoming call only when no other system-wide application would provide vibration (such as in mute mode). I know that this is somehow against the rules, for that an application should respect the users' settings, but the application is limited to some certain users with this need. I have tried two ways but neither of them are satisfying:

  1. Listen to the telephony state and directly trigger the vibration service with my own pattern (with Vibrator.vibrate()). This method is effective with no incoming calls yet randomly effective when the phone is in CALL_STATE_RINGING state and I guess it's because of the conflict with the system-wide application that actually handles the vibration upon incoming call.

  2. Judge whether the cellphone is vibrating upon an incoming call (with AudioManager.shouldVibrate()), and decide whether to change the vibrate settings (with AudioManager.setRingerMode() and AudioManager.setVibrateSetting()). If the vibrate settings are changed by my application, they are to be restored once the cellphone is back to CALL_STATE_IDLE state. This method, however, is still not functioning sometimes, without any sign of the reason.

I hope that someone could give some advice on this issue. Comments on these two ways or other suggest are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千紇 2025-01-01 18:20:03

您需要使用两种设置才能使手机振动。第一个是需要使用 AufioManager 设置的声音模式:

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(targetSoundMode);

第二部分没有正确记录,我相信这是代码中缺少的部分:

String VIBRATE_IN_SILENT_SETTING_NAME = "vibrate_in_silent";
Settings.System.putInt(getContentResolver(), VIBRATE_IN_SILENT_SETTING_NAME, 1);

使用 1 打开振动,使用 0 关闭振动。

要完全了解如何使用振动设置和模式,请查看以下链接:
http://hi-android.info/src/com/android /settings/SoundSettings.java.html

You need to play with two settings in order for your phone to vibrate. The first one is the sound mode which needs to be set by using AufioManager:

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(targetSoundMode);

The second part is not properly documented and I believe this is the missing part of your code:

String VIBRATE_IN_SILENT_SETTING_NAME = "vibrate_in_silent";
Settings.System.putInt(getContentResolver(), VIBRATE_IN_SILENT_SETTING_NAME, 1);

use 1 to turn vibrate on and 0 to turn vibrate off.

to fully understand how you should work with vibrate settings and mode take a look at the following link:
http://hi-android.info/src/com/android/settings/SoundSettings.java.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文