在android中接听电话时停止系统振动
我制作了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在 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) {}
}
试试这个:(从 Android 源借用并修改)
Try this: (borrowed and modified from Android Source)