出现音乐播放器通知时,禁止振动

时间:2018-12-31 15:54:48

标签: android foreground-service android-vibration

如何在出现通知时禁止振动?

我在startForground(int, int);类中使用了PlayerService

  • 我会在播放和暂停状态下更新我的通知,而通知更新会指出它开始振动,我想永久禁用它。

有解决方案吗?

注意:我尝试了很多方法,但是它又开始振动了。

builder.setDefault(Notification.DEFAULT_ALL);

builder.setVibrate(new long[]{0L})

uilder.setVibrate(new long[]{01, 01})

 NotificationManager notificationManager;
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel;
    builder = new NotificationCompat.Builder(this);
    String title = playlist().get(play_song_id).get(1);
    String content = getSongArtists();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        channel = new NotificationChannel("lawanmusic", "lawanmusic", NotificationManager.IMPORTANCE_HIGH);
        channel.setLightColor(getColor(R.color.colorAccent));
        channel.enableVibration(false);
        channel.setVibrationPattern(new long[]{ 01, 01 });
        channel.enableVibration(false);
        channel.setImportance(NotificationManager.IMPORTANCE_DEFAULT);
        builder.setChannelId(channel.getId());
        notificationManager.createNotificationChannel(channel);
    }
    builder.setDefaults(Notification.DEFAULT_ALL | -Notification.DEFAULT_VIBRATE).setVibrate(new long[]{ 0L });
    builder.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle().setShowActionsInCompactView(0,1,2).setCancelButtonIntent(null).setMediaSession(new MediaSessionCompat(this, "media").getSessionToken()).setShowActionsInCompactView());
    builder.setContentTitle(title);
    builder.setContentText(content);
    @DrawableRes int icon = R.drawable.play_icon_notification;
    builder.setSmallIcon(icon);
    builder.setLargeIcon(getSongCover());
    PendingIntent pendingIntent = PendingIntent.getActivity(this, Constants.NOTIFICATION_ID, new Intent(this, ActivityMain.class).putExtra("player", true), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    final PendingIntent prev = PendingIntent.getBroadcast(this, Constants.NOTIFICATION_ID, new Intent(this, PlayerReceiver.class).setAction(PREV_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.notification_prev, "Prev", prev);
    final PendingIntent pause = PendingIntent.getBroadcast(this, Constants.NOTIFICATION_ID, new Intent(this, PlayerReceiver.class).setAction(PAUSE_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.notification_pause, "Pause", pause);
    final PendingIntent next = PendingIntent.getBroadcast(this, Constants.NOTIFICATION_ID, new Intent(this, PlayerReceiver.class).setAction(NEXT_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.notification_next, "Next", next);
    final PendingIntent stop = PendingIntent.getBroadcast(this, Constants.NOTIFICATION_ID, new Intent(this, PlayerReceiver.class).setAction(STOP_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.notification_stop, "Player Off", stop);
    builder.setShowWhen(false);
    builder.build();
    notification.flags = Notification.DEFAULT_ALL | -Notification.DEFAULT_VIBRATE;
    notification.vibrate = null;
    notification = builder.build();
    startForeground(Constants.NOTIFICATION_ID, notification);

1 个答案:

答案 0 :(得分:1)

您可能正在使用IMPORTANCE_DEFAULT创建NotificationChannel,该文档说:

  

默认通知的重要性:在任何地方显示,发出噪音,但   不会从视觉上侵入。

您可能想要IMPORTANCE_LOW:

  

通知的重要性低:到处显示,但不具干扰性。

或IMPORTANCE_MIN:

  

没有声音并且没有出现在状态栏中

但是我不确定,因为我还没有看到围绕该服务的所有代码

Create and Manage Notification Channels