【问题标题】:Stop the system vibration on receive call in android在android中接听电话时停止系统振动
【发布时间】:2014-01-02 20:11:02
【问题描述】:

我创建了一个正在监听 PHONE_STATE 变化的 BroadcastReceiver。在 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 的第一种方法确实改变了振动的系统设置,但不会影响当前正在进行的设置。

有什么想法吗?

西蒙

【问题讨论】:

    标签: android broadcastreceiver vibration


    【解决方案1】:

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

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

    while ((System.currentTimeMillis() - timea)

    【讨论】:

      【解决方案2】:

      试试这个:(从 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);
      

      【讨论】:

      • 我以前见过该代​​码被剪断。这里有2个问题:1.手机没有处于静音模式,2.我找不到值Settings.System.VIBRATE_IN_SILENT。那应该是哪个版本的android?我正在使用 2.1 (7) 版
      • Settings.System.VIBRATE_IN_SILENT 应该存在于 2.1 中。它只是隐藏在文档中。常数不起作用吗?如果它不是处于静默模式,这也应该有效,但您必须尝试取消注释这两行之一。
      • VIBRATE_IN_SILENT 无法解决,文档说它可能会在未来的版本中被删除。我只是使用了字符串值“vibrate_in_silent”。这确实有效,但当然会在静音模式下切换手机,这并不是我的真正意图。铃声模式应该保持不变,只是应该关闭振动。
      • 所以你想让它响,但不振动?如果只是保持选项的问题,只需在短时间后将其翻转回来。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多