Android 使用 DowanloadManager 實現(xiàn)下載并獲取下載進度實例代碼
更新時間:2017年06月11日 10:55:09 投稿:lqh
這篇文章主要介紹了Android 使用 DowanloadManager 實現(xiàn)下載并獲取下載進度實例代碼的相關(guān)資料,需要的朋友可以參考下
Android 使用 DowanloadManager 實現(xiàn)下載并獲取下載進度實例代碼
實現(xiàn)代碼:
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.ImageView; import android.widget.TextView; import com.koolsee.gallery.adapter.RecommendAdapter; import com.koolsee.gallery.model.Recommend; import com.koolsee.gallery.widget.GalleryFlow; /** * 首頁 * * @author zengxiaotao */ public class testActivity 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.Request.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); } class DownloadChangeObserver extends ContentObserver { public DownloadChangeObserver(Handler handler) { super(handler); // TODO Auto-generated constructor stub } @Override public void onChange(boolean selfChange) { queryDownloadStatus(); } } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //這里可以取得下載的id,這樣就可以知道哪個文件下載完成了。適用與多個下載任務(wù)的監(jiān)聽 Log.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: // 正在下載,不做任何事情 Log.v("tag", "STATUS_RUNNING"); break; case DownloadManager.STATUS_SUCCESSFUL: // 完成 Log.v("tag", "下載完成"); // dowanloadmanager.remove(lastDownloadId); break; case DownloadManager.STATUS_FAILED: // 清除已下載的內(nèi)容,重新下載 Log.v("tag", "STATUS_FAILED"); dowanloadmanager.remove(lastDownloadId); break; } } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); unregisterReceiver(receiver); getContentResolver().unregisterContentObserver(downloadObserver); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android開發(fā)之日歷CalendarView用法示例
這篇文章主要介紹了Android開發(fā)之日歷CalendarView用法,簡單分析了日歷CalendarView組件的功能、屬性設(shè)置方法、界面布局、事件監(jiān)聽等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03ionic監(jiān)聽android返回鍵實現(xiàn)“再按一次退出”功能
本篇文章主要介紹了ionic監(jiān)聽android返回鍵實現(xiàn)“再按一次退出”功能,非常具有實用價值,需要的朋友可以參考下2018-02-02Android為textView設(shè)置setText的時候報錯的講解方案
今天小編就為大家分享一篇關(guān)于Android為textView設(shè)置setText的時候報錯的講解方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03WAC啟動Android模擬器 transfer error: Read-only file system錯誤解決方法
這篇文章主要為大家分享下WAC啟動Android模擬器時出現(xiàn)transfer error: Read-only file system 問題的解決方法2013-10-10