Android 使用 DowanloadManager 实现下载并获取下载进度实例代码
Android使用DowanloadManager实现下载并获取下载进度实例代码
实现代码:
packagecom.koolsee.gallery;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Timer;
importjava.util.TimerTask;
importandroid.app.Activity;
importandroid.app.DownloadManager;
importandroid.app.DownloadManager.Request;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.IntentFilter;
importandroid.database.ContentObserver;
importandroid.database.Cursor;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.os.Handler;
importandroid.util.Log;
importandroid.view.KeyEvent;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnKeyListener;
importandroid.view.View.OnTouchListener;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.AdapterView.OnItemSelectedListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
importandroid.widget.TextView;
importcom.koolsee.gallery.adapter.RecommendAdapter;
importcom.koolsee.gallery.model.Recommend;
importcom.koolsee.gallery.widget.GalleryFlow;
/**
*首页
*
*@authorzengxiaotao
*/
publicclasstestActivityextendsActivity{
privateDownloadManagerdowanloadmanager=null;
privateDownloadChangeObserverdownloadObserver;
privatelonglastDownloadId=0;
publicstaticfinalUriCONTENT_URI=Uri.parse("content://downloads/my_downloads");
@Override
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
StringserviceString=Context.DOWNLOAD_SERVICE;
dowanloadmanager=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Uriuri=Uri.parse("http://commonsware.com/misc/test.mp4");
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).mkdir();
lastDownloadId=dowanloadmanager.enqueue(newDownloadManager.Request(uri)
.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_MOBILE
|DownloadManager.Request.NETWORK_WIFI)
.setAllowedOverRoaming(false)
.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,"test.mp4"));
registerReceiver(receiver,newIntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
downloadObserver=newDownloadChangeObserver(null);
getContentResolver().registerContentObserver(CONTENT_URI,true,downloadObserver);
}
classDownloadChangeObserverextendsContentObserver{
publicDownloadChangeObserver(Handlerhandler){
super(handler);
//TODOAuto-generatedconstructorstub
}
@Override
publicvoidonChange(booleanselfChange){
queryDownloadStatus();
}
}
privateBroadcastReceiverreceiver=newBroadcastReceiver(){
@Override
publicvoidonReceive(Contextcontext,Intentintent){
//这里可以取得下载的id,这样就可以知道哪个文件下载完成了。适用与多个下载任务的监听
Log.v("tag",""+intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,0));
queryDownloadStatus();
}
};
privatevoidqueryDownloadStatus(){
DownloadManager.Queryquery=newDownloadManager.Query();
query.setFilterById(lastDownloadId);
Cursorc=dowanloadmanager.query(query);
if(c!=null&&c.moveToFirst()){
intstatus=c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
intreasonIdx=c.getColumnIndex(DownloadManager.COLUMN_REASON);
inttitleIdx=c.getColumnIndex(DownloadManager.COLUMN_TITLE);
intfileSizeIdx=
c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
intbytesDLIdx=
c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
Stringtitle=c.getString(titleIdx);
intfileSize=c.getInt(fileSizeIdx);
intbytesDL=c.getInt(bytesDLIdx);
//Translatethepausereasontofriendlytext.
intreason=c.getInt(reasonIdx);
StringBuildersb=newStringBuilder();
sb.append(title).append("\n");
sb.append("Downloaded").append(bytesDL).append("/").append(fileSize);
//Displaythestatus
Log.d("tag",sb.toString());
switch(status){
caseDownloadManager.STATUS_PAUSED:
Log.v("tag","STATUS_PAUSED");
caseDownloadManager.STATUS_PENDING:
Log.v("tag","STATUS_PENDING");
caseDownloadManager.STATUS_RUNNING:
//正在下载,不做任何事情
Log.v("tag","STATUS_RUNNING");
break;
caseDownloadManager.STATUS_SUCCESSFUL:
//完成
Log.v("tag","下载完成");
//dowanloadmanager.remove(lastDownloadId);
break;
caseDownloadManager.STATUS_FAILED:
//清除已下载的内容,重新下载
Log.v("tag","STATUS_FAILED");
dowanloadmanager.remove(lastDownloadId);
break;
}
}
}
@Override
protectedvoidonDestroy(){
//TODOAuto-generatedmethodstub
super.onDestroy();
unregisterReceiver(receiver);
getContentResolver().unregisterContentObserver(downloadObserver);
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!