自定义Android下载服务 - 每个文件提供进度通知行



我希望能够在通知栏中显示多个文件下载,也可以取消。

我已经实施了自定义服务,使用asynctasks并行执行多个下载。OnPublishProgress我正在尝试更新通知栏中的单个行,以显示每个文件的下载进度。在两天的时间里,我一直在尝试解决行闪烁,交换订单,有时只是空白或仅更新一行的问题。另外,敲击行取消例程并不总是有效。

这是我的代码:

    protected void showProgressNotification(final File item, int progress, boolean isDownloading) {
    String message = null;
    int smallIcon = 0;
    Bitmap largeIcon = null;
    int flags = 0;
    flags |= Notification.FLAG_ONGOING_EVENT; 
    //flags |= Notification.FLAG_FOREGROUND_SERVICE;
    //flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    //flags |= Notification.FLAG_AUTO_CANCEL; 
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(getApplicationContext());
    builder.setAutoCancel(true);
    if (progress == 100) {
        largeIcon = BitmapFactory.decodeResource(getResources(),
                O2FolderListAdapter.getIconForItem(item, false));
        smallIcon = R.drawable.ic_cloud_upto_date;
        if (isDownloading) {
            message = "Download completed. Tap to clear.";
        } else {
            message = "Upload completed. Tap to clear.";
        }
    } else if (progress >= 0) {
        largeIcon = BitmapFactory.decodeResource(getResources(),
                O2FolderListAdapter.getIconForItem(item, true));
        if (isDownloading) {
            smallIcon = R.drawable.ic_cloud_downloading;
            message = "Downloading: " + progress + "%. Tap to cancel.";
        } else {
            smallIcon = R.drawable.ic_cloud_uploading;
            message = "Uploading: " + progress + "%. Tap to cancel.";
        }
        builder.setProgress(100, progress, false);
    } else {
        largeIcon = BitmapFactory.decodeResource(getResources(),
                O2FolderListAdapter.getIconForItem(item, true));
        smallIcon = R.drawable.ic_cloud_conflict;
        if (isDownloading)
            message = "Cancelled download. Tap to clear.";
        else
            message = "Cancelled upload. Tap to clear.";
    }
    if (mResultIntent == null) {
        mResultIntent = new Intent(getApplicationContext(), CustomDownloadService.class);
        mResultIntent.addFlags(Notification.FLAG_ONGOING_EVENT);
    }
    mResultIntent.putExtra("cancel", item.getPath().hashCode());
    Log.d("O2AbstractDownloadService", "Setup task id " + item.GetPath().hashCode());
    if (mContentIntent == null)
        mContentIntent = PendingIntent.getService(getApplicationContext(), PI_REQ_CODE, mResultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(mContentIntent);
    builder.setLargeIcon(largeIcon);
    builder.setSmallIcon(smallIcon);
    builder.setContentTitle(item.GetName());
    builder.setContentText(message);
    //if (progress != 100)
        //builder.addAction(R.drawable.ic_action_dark_cancel, "Cancel", contentIntent);
    final Notification notification = builder.build();
    notification.flags = flags;
    notification.defaults = Notification.DEFAULT_LIGHTS;
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // Id allows you to update the notification later on.
    //mNotificationManager.notify(item.getPath().hashCode(), notification);
    //startForeground(item.getPath().hashCode(), notification);
    // only update notification every 100ms (unless cancel or complete)
    long notificationDelay = 100;
    long now = System.currentTimeMillis();
    if (mFutureCallTime == 0 || now > mFutureCallTime || progress == -1 || progress == 100) {
        startForeground(item.getPath().hashCode(), notification);
            //mNotificationManager.notify(item.GetPath().hashCode(), notification);
    } else
        Log.d("CustomDownloadService", "Called too often to notification");
    mFutureCallTime = now + notificationDelay;
}

因此,我正在尝试设置操作以在敲击通知时调用服务,并传递文件的ID以取消下载。谁能看到我在做错什么?我实际上可能做的是什么?在Xoom平板电脑上,通知闪烁很多,但是在Nexus 7上不经常频繁地在Nexus 7上持续交换行,这实际上是不可能取消您想要的下载。

任何建议都将不胜感激。

更新1:我认为这可能导致我的一个问题:Android Service.StartForeground不尊重通知ID唯一性

更新2:通过调用Builder.set.setwhen(fieldtime)修复了交换问题。显然,新的日期时间导致每次刷新行时行重新排序。只需要修复Xoom上的闪烁和" Tap要取消"功能。

更新3: Xoom上的闪烁是固定的,并限制了刷新的调用。最后,该代码防止通知被100ms更新一次。其余问题与取消有关。点击首次取消工作,但在后续文件上不起作用。我也无法清理行。

更新4:单一取消问题是由于结果级别在班级级别。每次我创建一个新通知时,ID都会绑定。我还将标志更改为Notification.flag_only_alert_once,仅使用。

所有问题均已解决。我在原始帖子中添加了更新。总之:1)请小心构建器。2)每100毫秒不要刷新一次3)设置正确的标志。

最新更新