2017-05-05 105 views
1

我正在使用下載管理器從網址下載文件。並且文件被成功下載。下載管理器進度在通知區域中不可見

問題

文件默默地下載,通知區域中的任何通知。

下載管理器在我的設備上運行android 6.0時顯示通知(帶進度條)。在我將設備更新到android 7.0後,下載管理器在通知區域上不顯示任何通知

這裏是我的代碼

Uri uri = Uri.parse("file://" + destination); 

url = "http:....."; //Valid File URL 

//Set up download manager request 

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
request.setDescription("Downloading " + file_name); 
request.setTitle("My Downloader"); 
request.setDestinationUri(uri); //URI is valid 

//Start the download 
DownloadManager manager = (DownloadManager) getContext() 
           .getSystemService(Context.DOWNLOAD_SERVICE); 

還加入request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);是沒有幫助我的情況。

構建信息

這裏是我的搖籃構建infformation。

minSdkVersion 22 
targetSdkVersion 23 
compileSdkVersion 25 
buildToolsVersion "25.0.2" 

回答

0

爲什麼這會發生在第一位?

問題的原因是Android設備下載管理器的通知被禁用。 (這是我的銀河S7新的默認 - SM930W8附帶了新的Android N(7.0)更新)

解決方案1 ​​

  1. 轉到設置>應用
  2. 在選項點擊「顯示系統應用
  3. 查找「下載管理器」,並打開它
  4. 點擊「ñ otifications'應用程序設置下
  5. 開關允許通知

上述完成步驟後上面的代碼用於與通知工作得很好下載。

的解決方案2

上述溶液不會與一個應用程序的一般分佈工作。我們不能告訴用戶做解決方案1.

添加您自己的小進度條來顯示您的活動中的下載百分比會讓用戶知道他/她所請求的文件正在下載。

儘量避免通知,因爲如果android下載管理器給出相同的通知,那麼它是多餘的。(顯示通知的默認設置可能會改變設備與設備之間的關聯關係)

所以這裏是代碼。

Uri uri = Uri.parse("file://" + destination); 

url = "http:....."; //Valid File URL 

//Set up download manager request 

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
request.setDescription("Downloading " + file_name); 
request.setTitle("My Downloader"); 
request.setDestinationUri(uri); //URI is valid 

//Start the download 
DownloadManager manager = (DownloadManager) getContext() 
           .getSystemService(Context.DOWNLOAD_SERVICE); 
final long downloadId = manager.enqueue(request); 

final int UPDATE_PROGRESS = 5020; 

final Handler handler = new Handler(){ 
    @Override 
     public void handleMessage(Message msg) { 
     if(msg.what==UPDATE_PROGRESS){ 
      String downloaded = String.format("%.2f MB", (double)((msg.arg1)/1024)/1024); 
      String total = String.format("%.2f MB", (double) (msg.arg2)/1024)/1024); 
      String status = downloaded + "/" + total; 
      pDialog.setTitleText(status); 
     } 
     super.handleMessage(msg); 
     } 
    }; 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      boolean downloading = true; 
      while (downloading) { 
       DownloadManager.Query q = new DownloadManager.Query(); 
       q.setFilterById(downloadId); 
       Cursor cursor = manager.query(q); 
       cursor.moveToFirst(); 
       int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); 
       int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); 
       if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) { 
        downloading = false; 
       } 
       //Post message to UI Thread 
       Message msg = handler.obtainMessage(); 
       msg.what = UPDATE_PROGRESS; 
       //msg.obj = statusMessage(cursor); 
       msg.arg1 = bytes_downloaded; 
       msg.arg2 = bytes_total; 
       handler.sendMessage(msg); 
       cursor.close(); 
      } 
     } 
}).start();