Android N - 下载管理器通知取消按钮

问题描述 投票:3回答:4

Android N在下载管理器通知中有一个新的取消按钮。

我想在我的应用程序中执行一些代码,以便在用户按下此按钮时停止进度条。如果有的话,调用哪种方法?

另请注意,Intent过滤器操作DownloadManager.ACTION_NOTIFICATION_CLICKED仅在用户单击通知本身时触发,而不是在用户单击“取消”按钮时触发。

 if_downloadManager = new IntentFilter();
    if_downloadManager.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    if_downloadManager.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);

    br_downloadManager = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                ....
            }

            if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
                // This code is not executed when the user presses the Cancel Button in the Download Manager Notification
            }       
        }
    };

提前致谢。

android android-notifications android-download-manager cancel-button
4个回答
0
投票

我在我的应用程序中遇到了同样的问题,我必须处理下载通知上的“取消”按钮并从本机下载应用程序下载删除。

事实证明,如果您使用intent过滤器注册接收器:DownloadManager.ACTION_DOWNLOAD_COMPLETE,则在启动取消或下载删除时始终会调用它。

那么,如何区分下载完成和下载删除?

嗯,这很简单:

  1. dmid获取已取消下载的下载管理器ID(Intent data),该handleReceive作为参数传递给BroadcastReceiverdmid函数。
  2. 使用该dmid查询DownloadManager的状态。
  3. DownloadManager将为该DownloadManager.STATUS_SUCCESSFULor返回null,false列的值将为https://github.com/edx/edx-app-android/blob/8a75d0dba6b8570956eac5c21c99ecd5020c81ae/OpenEdXMobile/AndroidManifest.xml#L265-L271进行上述下载。
  4. 一旦你知道这一点,你可以做任何你想做的事!

作为参考,您可以在这里看到我是如何做到的:

  1. 我的接收者在AndroidManifest.xml中的声明:https://github.com/edx/edx-app-android/blob/d986a9ab64e7a0f999024035ec6fcbdb3428f613/OpenEdXMobile/src/main/java/org/edx/mobile/module/download/DownloadCompleteReceiver.java#L50-L62
  2. 我的接收者处理这种情况的实际代码:qazxsw poi

0
投票

Malko,我没有找到解决方案,但我同时使用以下解决方法。我使用Android Handler每10秒运行一次resetProgressIfNoOngoingDMRequest(),如下所示:

public int numberOfOngoingDMRequest() {
    cursor = downloadManager.query(new Query());
    int res = cursor.getCount();
    cursor.close();
    return res;
}

public boolean resetProgressIfNoOngoingDMRequest() {
    if (numberOfOngoingDMRequest() == 0) {
        refreshUpdateAllButton(false);
        resetEpisodesDownloadIds();
        act.misc.notifyEpisodesDataSetChanged();
        return true;
    }
    return false;
}

不太好但是它能完成这项工作。我只在应用程序位于前台时执行此操作。


0
投票

另一种解决方案是使用ContentObserver

下载管理器的内容uri应该是content://downloads/my_downloads,我们可以监视这个数据库的更改。当您使用下载ID开始下载时,将创建一行content://downloads/my_downloads/{downloadId}。我们可以检查此光标以了解此任务是否被取消。如果返回的游标为空或为null,则在数据库中找不到记录,则此下载任务将被用户取消。

        // get the download id from DownloadManager#enqueue
        getContentResolver().registerContentObserver(Uri.parse("content://downloads/my_downloads"),
                true, new ContentObserver(null) {
                    @Override
                    public void onChange(boolean selfChange, Uri uri) {
                        super.onChange(selfChange, uri);
                        if (uri.toString().matches(".*\\d+$")) {
                            long changedId = Long.parseLong(uri.getLastPathSegment());
                            if (changedId == downloadId[0]) {
                                Log.d(TAG, "onChange: " + uri.toString() + " " + changedId + " " + downloadId[0]);
                                Cursor cursor = null;
                                try {
                                    cursor = getContentResolver().query(uri, null, null, null, null);
                                    if (cursor != null && cursor.moveToFirst()) {
                                        Log.d(TAG, "onChange: running");
                                    } else {
                                        Log.w(TAG, "onChange: cancel");
                                    }
                                } finally {
                                    if (cursor != null) {
                                        cursor.close();
                                    }
                                }
                            }
                        }
                    }
                });

看到答案here


0
投票

我的解决方案与MiaN KhaLiD非常相似。单击“取消”时,DownloadManager.ACTION_DOWNLOAD_COMPLETE始终为接收者。我在接收器中这样做:

      Cursor cursor = downloadManager.query(query);

      cursor.moveToFirst();

      int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
      int status;
      try {
        status = cursor.getInt(columnIndex);
        KLog.d("download status = " + status);
      } catch (CursorIndexOutOfBoundsException e) {
        KLog.d("cancelled from notification");
        return;
      }
© www.soinside.com 2019 - 2024. All rights reserved.