Progress notification for download service in android
Download progress notification for android service
Demo screenshot:
Demo screenshot:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void showProgressNotification(String caption, long completedUnits, long totalUnits) { | |
int percentComplete = 0; | |
if (totalUnits > 0) { | |
percentComplete = (int) (100 * completedUnits / totalUnits); | |
} | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
NotificationChannel channel = new NotificationChannel(CHANNEL_ID_DEFAULT, | |
"Default", | |
NotificationManager.IMPORTANCE_DEFAULT); | |
nm.createNotificationChannel(channel); | |
} | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID_DEFAULT) | |
.setSmallIcon(R.drawable.ic_file_upload_white_24dp) | |
.setContentTitle(getString(R.string.app_name)) | |
.setContentText("" + percentComplete + "%") | |
.setProgress(100, percentComplete, false) | |
.setOngoing(true) | |
.setAutoCancel(false); | |
NotificationManager manager = | |
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
manager.notify(PROGRESS_NOTIFICATION_ID, builder.build()); | |
} |
Comments
Post a Comment