android - 如何禁用/重新启用接收通知时发生的振动?

标签 android android-notifications android-vibration

我正在为收到通知时的特定功能定义自定义振动。

但是,当手机屏幕关闭时,自定义振动会与默认通知振动一起播放。

我尝试将手机置于静音模式并以编程方式将其从静音模式中移除,还尝试使用部分唤醒锁,怀疑当 CPU 关闭时,也会引发默认振动,但这两种方法似乎都没有去工作。

我还尝试将默认音频和振动设为静音,并在收到通知后完成我的任务后将其恢复,但这只能帮助屏蔽默认通知声音,而不是默认振动。

//Trying to mute the notification sound and vibration
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
 //Restoring the defaults
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, defaultNotifVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

请告诉我如何以编程方式禁用/启用默认通知振动。

最佳答案

尝试这样做

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this);

这将导致振动模式 使用通知生成器并删除设置的振动,这将禁用振动

这是我处理声音和振动的例子 例如:

Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(body)
                .setContentTitle(title)
                .setSound(soundUri)
                .setLargeIcon(icon)
                .setSound(soundUri)
                .setLights(Color.parseColor("#ffb400"), 50, 10)
                .setVibrate(new long[]{500, 500, 500, 500})
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

关于android - 如何禁用/重新启用接收通知时发生的振动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42185959/

相关文章:

javascript - Android WebView - Javascript 不会触发回调函数

android - 如何使用 Kotlin DSL 将选项传递给 JavaPluginExtension

android - 谷歌通知上的一些代码中的 "mId"(未知的外星人变量)是什么,这是一个谜

android - 如何创建/替换 Android.mk 文件以实例化 Notification.TvExtender()

java - Android 振动已弃用。如何在 Android>= API 26 中使用 VibrationEffect?

android - 是否有类似于铃声偏好的振动偏好?

android - 检测设备是否振动?

android - 如何在Google map 中标记纬度、经度?

java - 如何从异常跟踪堆栈中判断连接超时和套接字超时?

android - 如何在应用程序被杀死时在状态栏上保持通知?