2017-05-05 52 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();