0
你好,我有類的下載文件。他按預期工作,除非在下載完成後刪除通知。我注意到兩個相同的代碼段實際上給出了不同的東西。我的意思是現在我有以下行爲:我開始下載文件,我可以通過點擊通知取消它們。下載按預期取消,必須中止。但是如果等到下載完成。第二次通知將在第一次完成時中止,第一次通知將永久保留在通知欄中。我不知道我的錯誤在哪裏。 示例代碼:文件下載不良行爲通知android
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);
}
我一次啓動6個任務,並且他們全部下載成功。但問題是,即使在下載任務完成後,位於通知欄中的5個通知。 – Mrusful
我試圖下載5個文件,每個文件大約5MB,其中大部分受損。 請附加創建通知功能。 – Basbous
請看我的upd。我也有損壞文件,但這是我的失敗。這發生在我有相同的文件名時。文件被相互覆蓋。無論如何,我認爲真的會更好,做你喜歡的功能。它可以避免我的問題。 – Mrusful