在android中接听电话时停止系统振动

发布于 2024-12-01 09:44:38 字数 719 浏览 0 评论 0原文

我制作了一个 BroadcastReceiver 来监听 PHONE_STATE 的变化。在onReceive方法中,我想关闭系统振动器。我尝试了不同的方法,但到目前为止没有一个有效。

AudioManager audioManager = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
systemVibration = audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);

Vibrator vib = (Vibrator)ctx.getSystemService(Context.VIBRATOR_SERVICE);
vib.cancel();

System.putInt(ctx.getContentResolver(), System.VIBRATE_ON, 0);

或全部在一起。

AudioManager 的第一种方法确实改变了振动的系统设置,但它不会影响当前正在进行的振动。

有什么想法吗?

西蒙

i made a BroadcastReceiver that is listening for changes in the PHONE_STATE. in the onReceive method, i'd like to turn off the system vibrator. i tried different approaches, but non of them worked so far.

AudioManager audioManager = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
systemVibration = audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);

or

Vibrator vib = (Vibrator)ctx.getSystemService(Context.VIBRATOR_SERVICE);
vib.cancel();

or

System.putInt(ctx.getContentResolver(), System.VIBRATE_ON, 0);

or all of them together.

the first approach with the AudioManager really changes the system setting for the vibration, but it does not affect the currently ongoing one.

any ideas?

Simon

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

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

发布评论

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

评论(2

无尽的现实 2024-12-08 09:44:38

现在 Android 中不允许停止由其他进程启动的振动,因此这个 hack 可能会停止振动,或者会给你一种它已经停止振动的感觉。

长时间a = System.currentTimeMillis();
振动器 v = (振动器) getSystemService(Context.VIBRATOR_SERVICE);

while ((System.currentTimeMillis() - timea) < 15000) {
v.振动(1);
尝试 {
线程睡眠(10);
} catch (InterruptedException e) {}
}

Stopping Vibration started by other process is not allowed in android now and thus this hack could stop Vibration or it will give you a feel that it has stopped vibration.

long timea = System.currentTimeMillis();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

while ((System.currentTimeMillis() - timea) < 15000) {
v.vibrate(1);
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}

二智少女 2024-12-08 09:44:38

试试这个:(从 Android 源借用并修改)

AudioManager am = Context.getSystemService(Context.AUDIO_SERVICE);
boolean vibeInSilent = false;
int callsVibrateSetting = AudioManager.VIBRATE_SETTING_OFF;

Settings.System.putInt(getContentResolver(), 
        Settings.System.VIBRATE_IN_SILENT,
        vibeInSilent ? 1 : 0);

//mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
//or (not sure which one will work)
//mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

am.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
        callsVibrateSetting);

Try this: (borrowed and modified from Android Source)

AudioManager am = Context.getSystemService(Context.AUDIO_SERVICE);
boolean vibeInSilent = false;
int callsVibrateSetting = AudioManager.VIBRATE_SETTING_OFF;

Settings.System.putInt(getContentResolver(), 
        Settings.System.VIBRATE_IN_SILENT,
        vibeInSilent ? 1 : 0);

//mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
//or (not sure which one will work)
//mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

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