Hello i have class for download file. He work as expected except for remove notification when download is finished. I note what two identical sections of code in effect give different stuff. I mean now I have following behavior: I start download file and I can cancel them by click on notification. Download canceled as expected, the one that must be abort. But if wait until download will finish. Second notification will abort when first will finish and first notification will be stay in notification bar forever. I don’t know where my mistake.
Example code:
public class DownloadVkVideoFiles extends AsyncTask<String, Integer, String>{
private static String BROADCAST_ACTION = "com.yourshows.helper.DownloadVkVideoFile.CANCELID";
public DownloadVkVideoFiles(Context c, String title, int taskId) {
this.context = c;
this.notifyId = taskId; //this is unique notification Id
this.BROADCAST_ACTION += String.valueOf(taskId); //broadcast action for pending intent
}
@Override
protected void onPreExecute() {
// execute the status bar notification
createNotification();
super.onPreExecute();
IntentFilter filter = new IntentFilter();
filter.addAction(BROADCAST_ACTION);
context.registerReceiver(receiver, filter); //register Receiver for cancel download file
}
@Override
protected String doInBackground(String... params) { //download file}
@Override
public void onProgressUpdate(Integer... progress) {
notification.contentView.setProgressBar(R.id.progressBar, 100, progress[0], false);
// inform the progress bar of updates in progress
notificationManager.notify(notifyId, notification);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
notificationManager.cancel(notifyId); // close finished notification
unregisterReceiver();
LOG.send(LOG.I, TAG, "Notification finished with ID = " + notifyId);
}
@Override
protected void onCancelled() {
super.onCancelled();
notificationManager.cancel(notifyId);
unregisterReceiver();
LOG.send(LOG.I, TAG, "Notification finished with ID = " + notifyId);
}
}
UPD:
createNotification:
public void createNotification() {
notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent();
notificationIntent.setAction(BROADCAST_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.download_notification);
// TODO change to shows title
tickerText = context.getResources().getText(R.string.downloadTitle);
icon = android.R.drawable.stat_sys_download;
time = System.currentTimeMillis();
notification = new Notification(icon, tickerText, time);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
contentView.setImageViewResource(R.id.downloadImage, R.drawable.download);
contentView.setTextColor(R.id.title, notification_text_color);
contentView.setFloat(R.id.title, "setTextSize", notification_text_size - 3);
contentView.setTextViewText(R.id.title, title);
contentView.setProgressBar(R.id.progressBar, 100, 0, false);
notification.contentIntent = pendingIntent;
notification.contentView = contentView;
//notificationManager.notify(notifyId, notification);
service.startForeground(notifyId, notification);
}
I tried something like that in my project but if you try to download more than 5 files the the download files will behave unexpected the all files damaged.
so i change the structure to Service instead of AsyncTask.
downloads stored in queue in download one by one the notification bar contain Progress bar number of download and current file name and download percentage.