我正在为收到通知时的特定功能定义自定义振动。
但是,当手机屏幕关闭时,自定义振动会与默认通知振动一起播放。
我尝试将手机置于静音模式并以编程方式将其从静音模式中移除,还尝试使用部分唤醒锁,怀疑当 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/