iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android中DownloadManager实现文件下载实例详解
  • 251
分享到

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

Android 2022-06-06 04:06:00 251人浏览 薄情痞子
摘要

Android中DownloadManager实现文件下载 下载 创建下载链接 DownloadManager.Request request = new DownloadM

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



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

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编程使用WEBView实现文件下载功能的两种方法Android文件下载进度条的实现代码Android zip文件下载和解压实例Android实现文件下载进度显示功能Android 文件下载三种基本方式Android Retrofit文件下载进度显示问题的解决方法Android使用缓存机制实现文件下载及异步请求图片加三级缓存Android 将文件下载到指定目录的实现代码Android文件下载功能实现代码Android使用WebView实现文件下载功能


--结束END--

本文标题: Android中DownloadManager实现文件下载实例详解

本文链接: https://www.lsjlt.com/news/22078.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作