7

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

我已经实现了一个自定义服务,它使用 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 上则不经常闪烁。所有设备最终都会不断地交换行,这意味着几乎不可能取消您想要的下载。

任何建议将不胜感激。

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

更新 2:通过调用 builder.setWhen(fixedTime) 修复了换出问题。显然,新的 dateTime 会导致行在每次刷新时重新排序。只需修复 Xoom 上的闪烁和“点击取消”功能即可。

更新 3: Xoom 上的闪烁已通过限制刷新调用得到修复。最后的代码防止通知在每 100 毫秒内被更新一次以上。剩下的问题与取消有关。点击取消第一次有效,但不适用于后续文件。我也无法清除行。

更新 4:单个取消问题是由 resultIntent 字段处于类级别引起的。每次刷新通知时,当我创建一个新通知时,ID 都会绑定。我还将标志更改为仅 Notification.FLAG_ONLY_ALERT_ONCE 并且仅使用了 .notify() 而不是 startForeground()。

4

1 回答 1

2

所有问题都已修复。我已在原始帖子中添加了更新。总结: 1) 小心 builder.setWhen(fixedTime)。2)不要每 100 毫秒刷新一次以上 3)设置正确的标志。

于 2013-03-13T13:42:31.017 回答