当收到通知时,我正在为特定功能定义自定义振动.
我试图将手机置于静音模式,并以编程方式将其从静音模式中删除,并尝试使用部分唤醒锁,怀疑当cpu处于关闭状态时,也会抛出默认振动,但这两种方法似乎都不起作用.
我也尝试将默认音频和振动静音,并在完成任务后恢复通知,但这只能帮助屏蔽默认通知声音,而不是默认的振动.
//Trying to mute the notification sound and vibration audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,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}) .setAutoCancel(true) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0,notificationBuilder.build());