欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中DownloadManager實現(xiàn)文件下載實例詳解

 更新時間:2017年03月17日 10:29:24   投稿:lqh  
這篇文章主要介紹了Android中DownloadManager實現(xiàn)文件下載實例詳解的相關(guān)資料,需要的朋友可以參考下

Android中DownloadManager實現(xiàn)文件下載

下載

創(chuàng)建下載鏈接

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

設(shè)置允許下載的網(wǎng)絡(luò)環(huán)境

request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);

WIFI網(wǎng)絡(luò) : DownloadManager.Request.NETWORK_WIFI

移動網(wǎng)絡(luò) : DownloadManager.Request.NETWORK_MOBILE

Notification顯示下載進(jìn)度

// 在Notification顯示下載進(jìn)度
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
// 設(shè)置Title
request.setTitle("更新");
// 設(shè)置描述
request.setDescription("正在下載更新文件...");

設(shè)置保存路徑

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

下載

下載會返回一個進(jìn)程ID

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

取消下載

通過ID可以需要下載

downloadManager.remove(id);

下載完成的監(jiān)聽

下載完成,系統(tǒng)會發(fā)出廣播,通過注冊廣播監(jiān)聽者可以監(jiān)聽到下載完成

廣播的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狀態(tài)下下載
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
// 設(shè)置通知欄
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">

 <!-- 網(wǎng)絡(luò)權(quán)限 -->
 <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>

實現(xiàn)

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

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

相關(guān)文章

  • Android PowerManagerService省電模式策略控制

    Android PowerManagerService省電模式策略控制

    這篇文章主要介紹了Android PowerManagerService省電模式策略控制,本文基于前兩篇文章的基礎(chǔ)介紹展開詳情,感興趣的小伙伴可以參考一下
    2022-08-08
  • Android實現(xiàn)極簡打開攝像頭

    Android實現(xiàn)極簡打開攝像頭

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)極簡打開攝像頭,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android學(xué)習(xí)筆記(二)之電話撥號器

    Android學(xué)習(xí)筆記(二)之電話撥號器

    目前手機市場上android已經(jīng)具有強大的霸主地位,吸引了很多的追棒者,android學(xué)習(xí)越來越火熱,本文給大家介紹android學(xué)習(xí)筆記(二)之電話撥號器,感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • android獲取情景模式和鈴聲 實現(xiàn)震動、鈴聲提醒

    android獲取情景模式和鈴聲 實現(xiàn)震動、鈴聲提醒

    這篇文章主要介紹了android獲取情景模式和鈴聲,實現(xiàn)震動、鈴聲提醒,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android編程中HTTP服務(wù)用法詳解

    Android編程中HTTP服務(wù)用法詳解

    這篇文章主要介紹了Android編程中HTTP服務(wù)用法,結(jié)合實例形式較為詳細(xì)的分析了Android中http服務(wù)的具體步驟與相關(guān)使用技巧,需要的朋友可以參考下
    2016-01-01
  • Android報錯Didn‘t?find?class?“android.view.x“問題解決原理剖析

    Android報錯Didn‘t?find?class?“android.view.x“問題解決原理剖析

    這篇文章主要為大家介紹了Android報錯Didn‘t?find?class?“android.view.x“問題解決及原理剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android Studio使用Profiler來完成內(nèi)存泄漏的定位

    Android Studio使用Profiler來完成內(nèi)存泄漏的定位

    這篇文章主要介紹了Android Studio使用Profiler來完成內(nèi)存泄漏的定位,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android編寫文件瀏覽器簡單實現(xiàn)

    Android編寫文件瀏覽器簡單實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Android編寫文件瀏覽器簡單實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android 插件化處理方案詳解

    Android 插件化處理方案詳解

    這篇文章主要介紹了Android 插件化處理方案的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • Android app第三方支付寶支付接入教程

    Android app第三方支付寶支付接入教程

    這篇文章主要為大家分享了Android app第三方支付寶支付接入教程,介紹了支付寶支付步驟,對支付寶感興趣的小伙伴們可以參考一下
    2016-05-05

最新評論