Android 使用 DowanloadManager 实现下载并获取下载进度实例代码
程序员文章站
2023-12-05 23:30:16
android 使用 dowanloadmanager 实现下载并获取下载进度实例代码
实现代码:
package com.koolsee.gallery;...
android 使用 dowanloadmanager 实现下载并获取下载进度实例代码
实现代码:
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,这样就可以知道哪个文件下载完成了。适用与多个下载任务的监听 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: // 清除已下载的内容,重新下载 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); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: Python中列表元素转为数字的方法分析