如何获取下载管理器,下载取消回调?或者如何隐藏或删除此通知取消?

时间:2017-06-20 06:20:08

标签: android android-notifications android-download-manager

当下载视频时,会有取消选项取消下载。但是当我取消下载时,我不会从DownloadManager获得任何回调来更新应用UI。

此cancle选项仅适用于Android 7。

否则有任何隐藏或删除取消选项的选项。

enter image description here

1 个答案:

答案 0 :(得分:0)

在为DownloadManager.ACTION_DOWNLOAD_COMPLETE注册广播接收器后,您可以在DownloadManager广播的onReceive()方法中检查DownloadManager状态,如下所示 -

private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context arg0, Intent arg1) {
   // TODO Auto-generated method stub
   checkDownloadStatus();
  } 
 };

private void checkDownloadStatus(){

  // TODO Auto-generated method stub
  DownloadManager.Query query = new DownloadManager.Query();
  long id = preferenceManager.getLong(strPref_Download_ID, 0);
  query.setFilterById(id);
  Cursor cursor = downloadManager.query(query);
  if(cursor.moveToFirst()){
   int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
   int status = cursor.getInt(columnIndex);
   int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
   int reason = cursor.getInt(columnReason);

   switch(status){
   case DownloadManager.STATUS_FAILED:
    String failedReason = "";
    switch(reason){
    case DownloadManager.ERROR_CANNOT_RESUME:
     failedReason = "ERROR_CANNOT_RESUME";
     break;
    case DownloadManager.ERROR_DEVICE_NOT_FOUND:
     failedReason = "ERROR_DEVICE_NOT_FOUND";
     break;
    case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
     failedReason = "ERROR_FILE_ALREADY_EXISTS";
     break;
    case DownloadManager.ERROR_FILE_ERROR:
     failedReason = "ERROR_FILE_ERROR";
     break;
    case DownloadManager.ERROR_HTTP_DATA_ERROR:
     failedReason = "ERROR_HTTP_DATA_ERROR";
     break;
    case DownloadManager.ERROR_INSUFFICIENT_SPACE:
     failedReason = "ERROR_INSUFFICIENT_SPACE";
     break;
    case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
     failedReason = "ERROR_TOO_MANY_REDIRECTS";
     break;
    case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
     failedReason = "ERROR_UNHANDLED_HTTP_CODE";
     break;
    case DownloadManager.ERROR_UNKNOWN:
     failedReason = "ERROR_UNKNOWN";
     break;
    }

    Toast.makeText(AndroidDownloadManagerActivity.this, 
      "FAILED: " + failedReason, 
      Toast.LENGTH_LONG).show();
    break;
   case DownloadManager.STATUS_PAUSED:
    String pausedReason = "";

    switch(reason){
    case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
     pausedReason = "PAUSED_QUEUED_FOR_WIFI";
     break;
    case DownloadManager.PAUSED_UNKNOWN:
     pausedReason = "PAUSED_UNKNOWN";
     break;
    case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
     pausedReason = "PAUSED_WAITING_FOR_NETWORK";
     break;
    case DownloadManager.PAUSED_WAITING_TO_RETRY:
     pausedReason = "PAUSED_WAITING_TO_RETRY";
     break;
    }

    Toast.makeText(AndroidDownloadManagerActivity.this, 
      "PAUSED: " + pausedReason, 
      Toast.LENGTH_LONG).show();
    break;
   case DownloadManager.STATUS_PENDING:
    Toast.makeText(AndroidDownloadManagerActivity.this, 
      "PENDING", 
      Toast.LENGTH_LONG).show();
    break;
   case DownloadManager.STATUS_RUNNING:
    Toast.makeText(AndroidDownloadManagerActivity.this, 
      "RUNNING", 
      Toast.LENGTH_LONG).show();
    break;
   case DownloadManager.STATUS_SUCCESSFUL:

    Toast.makeText(AndroidDownloadManagerActivity.this, 
      "SUCCESSFUL", 
      Toast.LENGTH_LONG).show();
    downloadManager.remove(id);
    break;
   } 
  } 
 }