1. The notification bar is resident to ensure that the Service is the foreground process to prevent the service from being killed
2. Use the standard notification bar style
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
@Override public int onStartCommand(Intent intent, int flags, int startId) { LogUtil.d(TAG, "onStartCommand"); final Integer notificationID = 100; //Set notification information: final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL); notificationBuilder.setOngoing(true) .setSmallIcon(R.drawable.ic_launcher_round) .setContentTitle("Xiaomi Video") .setContentText("The Legend of Zhen Huan, Old Boy, etc. are being downloaded") .setProgress(100, 0, false); //Send the notification: final Notification notification = notificationBuilder.build(); startForeground(notificationID, notification); UIThread.uiPost(new Runnable() { @Override public void run() { if (progress == 100) { stopForeground(true); return; } notificationBuilder.setProgress(100, progress++, false); Notification notification = notificationBuilder.build(); notificationManager.notify(notificationID, notification); UIThread.uiPost(this,100); } }, 100); return START_STICKY; }
To solve the problem that the Android O (8.0) notification bar does not display, you need to set NotificationChannel for NotificationManager
/** * 8.0 and above need to increase the channel */ @RequiresApi(Build.VERSION_CODES.O) private fun createChannelIfNeeded() { if (Build.VERSION.SDK_INT >= 26) { val channel = NotificationChannel(NOTIFICATION_CHANNEL, "Xiaomi Video", NotificationManager.IMPORTANCE_LOW) channel.description = "Download" channel.enableLights(false) channel.enableVibration(false) //channel.importance = NotificationManager.IMPORTANCE_LOW //Set to low, there will be no sound in the notification bar mNotificationManager.createNotificationChannel(channel) } }