单击时,在状态栏中保留通知图标

时间:2016-02-10 14:34:28

标签: android android-notifications android-notification-bar

我希望在我的应用运行并执行某些操作时显示简单的通知。我想在用户点击通知时打开我的应用。 到目前为止,这种行为就像我想要的那样。 但是,当用户单击通知时,在打开应用程序并且通知仍然存在的同时,通知栏中的通知图标(向左滑动之前的时间)消失。编辑:我刚观察到,一旦任何其他通知(WhatsApp)到达,通知图标会再次出现在通知栏中。 我该如何防止这种行为?

通知如下:

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

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent intent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    builder.setContentTitle("Running")
            .setContentText("App is running.")
            .setSmallIcon(R.drawable.ic_lock_outline_black_24dp)
            .setOngoing(true)
            .setContentIntent(intent);
    notifyManager.notify(appRunningNotificationID, builder.build());

1 个答案:

答案 0 :(得分:0)

看下面的代码:

           TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Running")
                .setContentText("App is running")
                .setTicker("My app")
                .setContentIntent(resultPendingIntent);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = mBuilder.build();
        long[] vibrate = { 500,1000 };
        notification.vibrate = vibrate;
        mNotificationManager.notify(appRunningNotificationID, notification);