国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中DownloadManager實現文件下載實例詳解

2019-12-12 03:21:02
字體:
來源:轉載
供稿:網友

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);// 設置Titlerequest.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();  } }}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭阳县| 芦溪县| 嘉黎县| 夏津县| 禹州市| 安阳市| 肃宁县| 南平市| 黄陵县| 无为县| 汨罗市| 昌吉市| 海淀区| 德化县| 三河市| 遂昌县| 清徐县| 枞阳县| 黄骅市| 都江堰市| 江西省| 河曲县| 兴仁县| 封开县| 涞水县| 景谷| 武城县| 沙洋县| 普兰店市| 涪陵区| 清镇市| 泉州市| 新巴尔虎右旗| 青海省| 日土县| 高淳县| 鹤峰县| 鹤峰县| 仙居县| 马鞍山市| 乌苏市|