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

时间:2012-10-26 13:58:06

标签: android service notifications android-service android-notifications

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

我已经实现了一个自定义服务,它使用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 does NOT respect notification id uniqueness

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

更新3:修复了Xoom上的闪烁,限制了刷新的调用。最后的代码可以防止通知在100ms内多次更新。剩下的问题与取消有关。要取消的点按第一次工作,但不适用于后续文件。我也无法清除行。

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

1 个答案:

答案 0 :(得分:2)

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