Android uses dowanloadmanager to download objects and obtain the download progress.

Source: Internet
Author: User

Package com. koolsee. gallery; import java. util. arrayList; import java. util. list; import java. util. timer; import java. util. timerTask; import android. app. activity; import android. app. downloadManager; import android. app. downloadManager. request; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. dat Abase. contentObserver; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. OS. handler; import android. util. log; import android. view. keyEvent; import android. view. motionEvent; import android. view. view; import android. view. view. onKeyListener; import android. view. view. onTouchListener; import android. widget. adapterView; import Android. widget. adapterView. onItemClickListener; import android. widget. adapterView. onItemSelectedListener; import android. widget. button; import android. widget. imageView; import android. widget. textView; import com. koolsee. gallery. adapter. recommendAdapter; import com. koolsee. gallery. model. recommend; import com. koolsee. gallery. widget. galleryFlow;/*** homepage ** @ author zengxiaotao */public class testA Ctivity extends Activity {private DownloadManager dowanloadmanager = null; private DownloadChangeObserver downloadObserver; private long lastDownloadId = 0; public static final Uri CONTENT_URI = Uri. parse ("content: // downloads/my_downloads"); @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. Test); String serviceString = Context. DOWNLOAD_SERVICE; dowanloadmanager = (DownloadManager) getSystemService (DOWNLOAD_SERVICE); Uri uri = Uri. parse ("http://commonsware.com/misc/test.mp4"); Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_DOWNLOADS ). mkdir (); lastDownloadId = dowanloadmanager. enqueue (new DownloadManager. request (uri ). setAllowedNetworkTypes (DownloadManager. requ Est. NETWORK_MOBILE | DownloadManager. request. NETWORK_WIFI ). setAllowedOverRoaming (false ). setDestinationInExternalPublicDir (Environment. DIRECTORY_DOWNLOADS, "test.mp4"); registerReceiver (receiver, new IntentFilter (DownloadManager. ACTION_DOWNLOAD_COMPLETE); downloadObserver = new DownloadChangeObserver (null); getContentResolver (). registerContentObserver (CONTENT_URI, true, downloadObserver);} cl Ass DownloadChangeObserver extends ContentObserver {public DownloadChangeObserver (Handler handler) {super (handler); // TODO Auto-generated constructor stub} @ Override public void onChange (boolean selfChange) {queryDownloadStatus () ;}} private BroadcastReceiver receiver ER = new BroadcastReceiver () {@ Override public void onReceive (Context context, Intent intent) {// The download id can be obtained here, in this way, you can know which file has been downloaded. Applicable to the monitoring logs of multiple download tasks. v ("tag", "" + intent. getLongExtra (DownloadManager. EXTRA_DOWNLOAD_ID, 0); queryDownloadStatus () ;}}; private void queryDownloadStatus () {DownloadManager. query query = new DownloadManager. query (); query. setFilterById (lastDownloadId); Cursor c = dowanloadmanager. query (query); if (c! = Null & c. moveToFirst () {int status = c. getInt (c. getColumnIndex (DownloadManager. COLUMN_STATUS); int reasonIdx = c. getColumnIndex (DownloadManager. COLUMN_REASON); int titleIdx = c. getColumnIndex (DownloadManager. COLUMN_TITLE); int fileSizeIdx = c. getColumnIndex (DownloadManager. COLUMN_TOTAL_SIZE_BYTES); int bytesDLIdx = c. getColumnIndex (DownloadManager. COLUMN_BYTES_DOWNLOADED_SO_FAR); String title = C. getString (titleIdx); int fileSize = c. getInt (fileSizeIdx); int bytesDL = c. getInt (bytesDLIdx); // Translate the pause reason to friendly text. int reason = c. getInt (reasonIdx); StringBuilder sb = new StringBuilder (); sb. append (title ). append ("\ n"); sb. append ("Downloaded "). append (bytesDL ). append ("/"). append (fileSize); // Display the status Log. d ("tag", sb. toString (); switch (status) {case Dow NloadManager. STATUS_PAUSED: Log. v ("tag", "STATUS_PAUSED"); case DownloadManager. STATUS_PENDING: Log. v ("tag", "STATUS_PENDING"); case DownloadManager. STATUS_RUNNING: // download. v ("tag", "STATUS_RUNNING"); break; case DownloadManager. STATUS_SUCCESSFUL: // complete Log. v ("tag", "download completed"); // dowanloadmanager. remove (lastDownloadId); break; case DownloadManager. STATUS_FAILED: // clear the downloaded content and download the Log again. v ("tag ", "STATUS_FAILED"); dowanloadmanager. remove (lastDownloadId); break ;}}@ Override protected void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); unregisterReceiver (receiver); getContentResolver (). unregisterContentObserver (downloadObserver) ;}} package com. koolsee. gallery; import java. util. arrayList; import java. util. list; import java. util. timer; import java. util. timerTask; import Android. app. activity; import android. app. downloadManager; import android. app. downloadManager. request; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. database. contentObserver; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. OS. handler; import android. util. log; import android. view. keyEvent; import android. view. motionEvent; import android. view. view; import android. view. view. onKeyListener; import android. view. view. onTouchListener; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. adapterView. onItemSelectedListener; import android. widget. button; import android. widget. im AgeView; import android. widget. textView; import com. koolsee. gallery. adapter. recommendAdapter; import com. koolsee. gallery. model. recommend; import com. koolsee. gallery. widget. galleryFlow;/*** homepage ** @ author zengxiaotao */public class testActivity extends Activity {private DownloadManager dowanloadmanager = null; private DownloadChangeObserver downloadObserver; private long lastDownloadId = 0; public s Tatic final Uri CONTENT_URI = Uri. parse ("content: // downloads/my_downloads"); @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. test); String serviceString = Context. DOWNLOAD_SERVICE; dowanloadmanager = (DownloadManager) getSystemService (DOWNLOAD_SERVICE); Uri uri = Uri. parse ("http: // commonsw Are.com/misc/test.mp4 "); Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_DOWNLOADS ). mkdir (); lastDownloadId = dowanloadmanager. enqueue (new DownloadManager. request (uri ). setAllowedNetworkTypes (DownloadManager. request. NETWORK_MOBILE | DownloadManager. request. NETWORK_WIFI ). setAllowedOverRoaming (false ). setDestinationInExternalPublicDir (Environment. DIRECTORY_DOWNLOADS, "test. mp 4 "); registerReceiver (receiver, new IntentFilter (DownloadManager. ACTION_DOWNLOAD_COMPLETE); downloadObserver = new DownloadChangeObserver (null); getContentResolver (). response (CONTENT_URI, true, downloadObserver);} class DownloadChangeObserver extends ContentObserver {public DownloadChangeObserver (Handler handler) {super (handler); // TODO Auto-generated constructor stub} @ Overr Ide public void onChange (boolean selfChange) {queryDownloadStatus () ;}} private BroadcastReceiver receiver ER = new BroadcastReceiver () {@ Override public void onReceive (Context context, Intent intent) {// The download id can be obtained here, so that you can know which file has been downloaded. Applicable to the monitoring logs of multiple download tasks. v ("tag", "" + intent. getLongExtra (DownloadManager. EXTRA_DOWNLOAD_ID, 0); queryDownloadStatus () ;}}; private void queryDownloadStatus () {DownloadManager. query query = new DownloadManager. query (); query. setFilterById (lastDownloadId); Cursor c = dowanloadmanager. query (query); if (c! = Null & c. moveToFirst () {int status = c. getInt (c. getColumnIndex (DownloadManager. COLUMN_STATUS); int reasonIdx = c. getColumnIndex (DownloadManager. COLUMN_REASON); int titleIdx = c. getColumnIndex (DownloadManager. COLUMN_TITLE); int fileSizeIdx = c. getColumnIndex (DownloadManager. COLUMN_TOTAL_SIZE_BYTES); int bytesDLIdx = c. getColumnIndex (DownloadManager. COLUMN_BYTES_DOWNLOADED_SO_FAR); String title = c. getString (titleIdx); int fileSize = c. getInt (fileSizeIdx); int bytesDL = c. getInt (bytesDLIdx); // Translate the pause reason to friendly text. int reason = c. getInt (reasonIdx); StringBuilder sb = new StringBuilder (); sb. append (title ). append ("\ n"); sb. append ("Downloaded "). append (bytesDL ). append ("/"). append (fileSize); // Display the status Log. d ("tag", sb. toString (); switch (status) {case DownloadManager. STATUS_PAUSED: Log. v ("tag", "STATUS_PAUSED"); case DownloadManager. STATUS_PENDING: Log. v ("tag", "STATUS_PENDING"); case DownloadManager. STATUS_RUNNING: // download. v ("tag", "STATUS_RUNNING"); break; case DownloadManager. STATUS_SUCCESSFUL: // complete Log. v ("tag", "download completed"); // dowanloadmanager. remove (lastDownloadId); break; case DownloadManager. STATUS_FAILED: // clear the downloaded content and download the Log again. v ("tag", "STATUS_FAILED"); dowanloadmanager. remove (lastDownloadId); break ;}}@ Override protected void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); unregisterReceiver (receiver); getContentResolver (). unregisterContentObserver (downloadObserver );}}

 

Related Article

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.