通知文件下载不良行为android

时间:2012-01-24 06:17:32

标签: android notifications

您好我有下载文件的课程。除了下载完成后删除通知外,他按预期工作。我注意到两个相同的代码部分实际上给出了不同的东西。我的意思是现在我有以下行为:我开始下载文件,我可以点击通知取消它们。下载按预期取消,必须中止。但如果等到下载完成。第一次通知将在第一次完成时中止,第一次通知将永久保留在通知栏中。我不知道我的错误在哪里。 示例代码:

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);
    }

1 个答案:

答案 0 :(得分:0)

我在我的项目中尝试过类似的东西,但是如果你尝试下载超过5个文件,下载文件就会出现意外情况,所有文件都会损坏。 所以我将结构更改为Service而不是AsyncTask。 下载存储在队列中的下载逐个通知栏包含下载进度条的编号和当前文件名和下载百分比。