android - 以编程方式将android中的通知振动强度降低为零

标签 android notifications

在我的应用程序中,我想在收到通知时关闭振动。我可以将通知声音静音,但无法停止振动。

要使通知声音静音,我使用了以下代码:

 if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) != value) {
        audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, value, 0);
        Utils.log("Volume Changed to " + value);
    }

请建议如何将通知振动强度更改为零或建议一些其他方法来在通知到达时取消振动。

audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
        AudioManager.VIBRATE_SETTING_OFF);

setVibrateSetting 无效且已弃用

我可以通过以下方式手动更改三星设备中通知的振动强度:

  1. 转到设置
  2. 转到“我的设备”标签
  3. 点击声音并打开“振动强度”
  4. 选择来电、通知和触觉反馈的振动强度

但是我怎样才能以编程方式做到这一点呢?请帮忙。

最佳答案

通常在手机和类似设备中振动由 small dis-balanced motor 实现,其中 causes the vibration when its powered on 。 该电机通常除了开/关外没有其他控制,因此您可以控制振动持续时间,而不是水平。

在这种情况下,您可以使用 Pulse-width modulation (PWM) 方法(如图所示)来控制振动强度:设置一些周期 T(例如 1 秒),第一次为 900 毫秒( tv1) 将振动设置为开和 100 毫秒 (ts1) - 保持振动关闭。超过 800 毫秒 (tv2) 将振动设置为打开,超过 200 毫秒 (ts2) - 保持振动关闭等,直到振动时间变为 0。

PWM vibration control

Vibrator 类中实现的 Android 振动器控件。

正如 @Rohit J 所建议的那样,您可以为此使用振动模式(描述为 here ),例如:

long[] pattern = {
        0,     // start vibrate immediately
        900,   // vibrate 1 900 ms
        100,   // silence 1 100 ms
        800,   // vibrate 2 800 ms
        200,   // silence 2 200 ms
        700,
        300,
        600,
        400,
        500,
        500,
        400,
        600,
        300,
        700,
        200,
        800,
        100,
        900,
};

并将其添加到Notification:

Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
        ...
        .setVibrate(pattern)
        ...;

注意!您还可以创建(手动或以编程方式)更复杂的振动控制模式,以实现更平滑的强度变化(例如,不创建长的实心振动脉冲,而是创建许多短的振动持续时间,但不减少振动的持续时间,而是减少脉冲的数量,例如 Pulse-density modulation ) .

关于android - 以编程方式将android中的通知振动强度降低为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40690376/

相关文章:

java - 即使更改后,Android 通知图标仍保留默认图标

android - 如何用手指 ontouch 事件 android 制作按钮动画?

java - 如何在关闭应用程序后停止 Intent 服务

android - 如何使用metaio SDK添加自己的POI

java - 在 android 中选择性别,如 radioButton

Android Camera API - 奇怪的缩放效果

ios - 使应用通知名称与应用名称不同

ios - 如何显示 iOS 推送通知弹出窗口?

javascript - 模拟通知 API

ios - Swift didReceiveRemoteNotification 在应用程序中触发