网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在Android中利用DownloadManager实现一个文件下载功能

本篇文章为大家展示了怎么在Android中利用DownloadManager实现一个文件下载功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

10年积累的网站设计制作、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先制作网站后付款的网站建设流程,更有龙华免费网站建设让你可以放心的选择与我们合作。

Android中DownloadManager实现文件下载

创建下载链接

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

设置允许下载的网络环境

request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);

WIFI网络 : DownloadManager.Request.NETWORK_WIFI

移动网络 : DownloadManager.Request.NETWORK_MOBILE

Notification显示下载进度

// 在Notification显示下载进度
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
// 设置Title
request.setTitle("更新");
// 设置描述
request.setDescription("正在下载更新文件...");

设置保存路径

private static final String DIR = "AutoUpdate";
private static final String APK = "MyHome.apk";
private static final String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + DIR + "/" + APK;

request.setDestinationInExternalPublicDir(DIR, APK);

下载

下载会返回一个进程ID

DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = downloadManager.enqueue(request);

取消下载

通过ID可以需要下载

downloadManager.remove(id);

下载完成的监听

下载完成,系统会发出广播,通过注册广播监听者可以监听到下载完成

广播的Action为DownloadManager.ACTION_DOWNLOAD_COMPLETE

/**
 * Broadcast intent action sent by the download manager when the user clicks on a running
 * download, either from a system notification or from the downloads UI.
 */
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public final static String ACTION_NOTIFICATION_CLICKED =
  "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";

Code

下载

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
// WIFI状态下下载
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
// 设置通知栏
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setTitle("更新");
request.setDescription("正在下载更新文件...");
// 存放路径
request.setDestinationInExternalPublicDir(DIR, APK);

// 开始下载
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = downloadManager.enqueue(request);

广播接收者

注册




 
 
 
 

 

 
  
   
    

    
   

  

  
   
    
   
  
 

实现

package com.example.kongqingwei.downloadmanagerdemo;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * Created by kongqingwei on 2016/12/19.
 * 广播接收者
 */
public class AutoUpdateBroadcastReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
  if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
   Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show();
   boolean isInstalled = AutoUpdater.installApk();
   Toast.makeText(context, isInstalled ? "安装成功" : "安装失败", Toast.LENGTH_SHORT).show();
  }
 }
}

上述内容就是怎么在Android中利用DownloadManager实现一个文件下载功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


文章标题:怎么在Android中利用DownloadManager实现一个文件下载功能
标题路径:http://bjjierui.cn/article/iijhio.html

其他资讯