Is it possible to reduce notification time in Android?

I use the following code to show a notification on my Android phone. I want to reduce the time it takes to display a notification. Now he spends about 2 seconds on showing, then he is hidden. I want to reduce the time from 2 seconds to 1 second. Is it possible?

Notification notification = new NotificationCompat.Builder(context)
                    .setContentTitle(context.getString(R.string.app_name))
                    .setContentText("Notification")
                    .setAutoCancel(false)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                    .build();

NotificationManager notificationManager = getNotificationManager(context);
notificationManager.notify(SAFER_DISCONNECTED_NOTIFICATION_ID, notification);
+4
source share
2 answers

Now it shows about 2 seconds to show, then it is hidden.

There is no specific guarantee as to the time during which it Notificationwill be displayed in heads-up mode, which, I assume, is what you are talking about.

, 0 Android 4.4 , -. , NotificationListenerService, , , .

?

, . , .

+2

.

, :

1- :

new java.util.Timer().schedule(
    new java.util.TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new   Runnable() {
                public void run() {
                    //your code for cancel notification(s)
                }
           });
        }
    }, 
1000);

2- :

NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(NOTIF_ID);

:

nMgr.cancelAll();
0

Source: https://habr.com/ru/post/1661697/


All Articles