欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中DownloadManager实现文件下载实例详解

程序员文章站 2024-02-10 12:22:34
android中downloadmanager实现文件下载 下载 创建下载链接 downloadmanager.request request = new d...

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);

广播接收者

注册

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.kongqingwei.downloadmanagerdemo">

 <!-- 网络权限 -->
 <uses-permission android:name="android.permission.internet"/>
 <uses-permission android:name="android.permission.read_external_storage"/>
 <uses-permission android:name="android.permission.write_external_storage"/>

 <uses-permission android:name="android.permission.manage_users"/>

 <application
  android:allowbackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:supportsrtl="true"
  android:theme="@style/apptheme">
  <activity android:name=".mainactivity">
   <intent-filter>
    <action android:name="android.intent.action.main"/>

    <category android:name="android.intent.category.launcher"/>
   </intent-filter>

  </activity>

  <receiver android:name=".autoupdatebroadcastreceiver">
   <intent-filter>
    <action android:name="android.intent.action.download_complete"/>
   </intent-filter>
  </receiver>
 </application>

</manifest>

实现

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();
  }
 }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!