Downloadmanager in Android

Source: Internet
Author: User

Downloadmanager use

Downloadmanager is a download manager provided by Android

Cons: No support for breakpoint continuation

How to use:

  1. To create an instance:

    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
  2. To create a download task:

    Downloadmanager. RequestRequest = new Downloadmanager. Request(Uri. Parse(URL));Specify Save Address Request. Setdestinationinexternalpublicdir("Chuyi","meizhi.apk");Set the network conditions that are allowed to download request. Setallowednetworktypes(Downloadmanager. Request. NETWORK_wifi);Set the behavior of the notification bar request. Setnotificationvisibility(Downloadmanager. Request. VISIBILITY_visible_notify_completed);This download task is uniquely identified by ID long id = Manager. Enqueue(Request);
  3. To delete a download task:

    manager.remove(id);
  4. Query Download Task:

    Downloadmanager. Queryquery = new Downloadmanager. Query();Query. Setfilterbyid(ID);Cursor cursor = Manager. Query(query);if (cursor. Movetofirst()) {String filename = cursor. getString(cursor. Getcolumnindex(Downloadmanager. COLUMN_local_filename));String Fileuri = cursor. getString(cursor. Getcolumnindex(Downloadmanager. COLUMN_local_uri));Toast. Maketext(Context,"Download Complete ... name:"+filename+", Uri:"+fileuri,toast. LENGTH_short). Show();}else {//todo}cursor. Close();
  5. Query Download Progress:

    Downloadmanager. Queryquery = new Downloadmanager. Query(). Setfilterbyid(Downloadid);Cursor c = null;try {c = Downloadmanager. Query(query);if (c! = null && C. Movetofirst()) {int downloadedbytes = c. GetInt(c. Getcolumnindexorthrow(Downloadmanager. COLUMN_bytes_downloaded_so_far));int totalbytes = C. GetInt(c. Getcolumnindexorthrow(Downloadmanager. COLUMN_total_size_bytes));int state = C. GetInt(c. Getcolumnindex(Downloadmanager. COLUMN_status));}} finally {if (c! = null) {C. Close();}}
  6. Listen for download end notifications:

    You can listen to the notification of the end of the download by receiving DOWNLOADMANAGER.ACTION_DOWNLOAD_COMPLETE broadcasts

    new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);    new BroadcastReceiver(){      publicvoidonReceive(Context c,Intent i){          long downId = i.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);      }    }    context.registerReceiver(mReceiver,filter);
  7. Listen for download progress:

    There are three main kinds of programs: 1. Filereceiver 2.ContentObserver 3. Scheduled Tasks

    Class Downloadchangeobserver extends Contentobserver { Public Downloadchangeobserver(){Super(handler);}@Override Public void OnChange(BooleanSelfchange) {//Query Progress}}//in ActivityPrivateDownloadchangeobserver Downloadobserver;@Overrideprotected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.download_manager_demo); downloadobserver =NewDownloadchangeobserver ();}@Overrideprotected void Onresume() {Super. Onresume ();/** Observer Download Change **/Getcontentresolver (). Registercontentobserver (Downloadmanagerpro.content_uri,true, downloadobserver);}@Overrideprotected void OnPause() {Super. OnPause (); Getcontentresolver (). Unregistercontentobserver (Downloadobserver);}

    This practice may have some loss of performance, as it will constantly trigger OnChange

    Recommended use of Scheduledexecutorservice

    publicstatic ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3new Runnable() {    @Override    publicvoidrun() {        updateView();    03, TimeUnit.SECONDS);

    Resources:

      1. http://www.trinea.cn/android/android-downloadmanager/
      2. http://www.trinea.cn/android/android-downloadmanager/
      3. Https://github.com/Trinea/android-common/blob/master/src/cn/trinea/android/common/util/DownloadManagerPro.java

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Downloadmanager in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.