需要将通知显示始终显示在通知栏的顶部,如音乐播放器

时间:2017-11-07 12:40:30

标签: android notifications

我已尝试使用自定义通知下面的代码,但它无效。我想在我的自定义通知中显示计数器,如音乐播放器。

final RemoteViews remoteViews = new RemoteViews(getPackageName(),
                R.layout.layout_workout);

        remoteViews.setTextViewText(R.id.layout_workout_tv_name, type + " " + videoList.get(position).getName());
        remoteViews.setTextViewText(R.id.layout_workout_tv_time, strTime);

        final Intent pauseWorkout = new Intent();
        pauseWorkout.setAction(ACTION_PAUSE);
        final PendingIntent pauseWorkoutPendingIntent = PendingIntent.getBroadcast(this, 101, pauseWorkout, PendingIntent.FLAG_UPDATE_CURRENT);


        final Intent resumeWorkout = new Intent();
        resumeWorkout.setAction(ACTION_RESUME);
        final PendingIntent resumeWorkoutPendingIntent = PendingIntent.getBroadcast(this, 102, resumeWorkout, PendingIntent.FLAG_UPDATE_CURRENT);


        final Intent skipWorkout = new Intent();
        skipWorkout.setAction(ACTION_SKIP);
        final PendingIntent skipExercisePendingIntent = PendingIntent.getBroadcast(this, 103, skipWorkout, PendingIntent.FLAG_UPDATE_CURRENT);

        final Intent completeSetWorkout = new Intent();
        completeSetWorkout.setAction(ACTION_COMPLETE_SET);
        final PendingIntent completeSetPendingIntent = PendingIntent.getBroadcast(this, 104, completeSetWorkout, PendingIntent.FLAG_UPDATE_CURRENT);


        final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(getNotificationIcon())
                .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                .setAutoCancel(false)
                .setContent(remoteViews)
                .setShowWhen(false)

                .setOngoing(true);

        notificationBuilder.setWhen(System.currentTimeMillis());


        remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_complete_set, completeSetPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_pause, pauseWorkoutPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_resume, resumeWorkoutPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_skip, skipExercisePendingIntent);

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

enter image description here

我也尝试了下面的代码

.setPriority(Notification.PRIORITY_MAX)

1 个答案:

答案 0 :(得分:0)

对于优先级较高的通知,请使用以下代码段...

NotificationCompat.Builder mBuilder =
    (NotificationCompat.Builder) new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.some_small_icon)
    .setContentTitle("Title")
    .setContentText("This is a test notification with MAX priority")
    .setPriority(Notification.PRIORITY_MAX);