Aandroid 3.2 加入了DownloadManager ,这里举例使用方法。

layout添加两个个button,两个txtview

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <Button
  9. android:id="@+id/start"
  10. android:text="StartDownload"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:onClick="startDownload"
  14. />
  15. <Button
  16. android:id="@+id/query"
  17. android:text="QueryStatus"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:onClick="queryDownloadStatus"
  21. />
  22. <Button
  23. android:id="@+id/del"
  24. android:text="deldownload"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:onClick="delDownloads"
  28. />
  29. <TextViewandroid:id="@+id/tvsize"
  30. android:text="file"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"/>
  33. <TextViewandroid:id="@+id/tvinfo"
  34. android:text="file"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"/>
  37. </LinearLayout>

        
  1. packagecom.download;
  2. importandroid.app.Activity;
  3. importandroid.app.DownloadManager;
  4. importandroid.app.DownloadManager.Request;
  5. importandroid.content.Intent;
  6. importandroid.database.Cursor;
  7. importandroid.net.Uri;
  8. importandroid.os.Bundle;
  9. importandroid.os.Environment;
  10. importandroid.os.Handler;
  11. importandroid.os.Message;
  12. importandroid.util.Log;
  13. importandroid.view.View;
  14. importandroid.widget.TextView;
  15. importandroid.widget.Toast;
  16. publicclassDWManagerThreadextendsActivity{
  17. privateDownloadManagermgr=null;
  18. privatelonglastDownload=-1L;
  19. TextViewtvdwsize;
  20. TextViewtvdwinfo=null;
  21. privateStringstrUrl="http://dl.google.com/android/ndk/android-ndk-r6-linux-x86.tar.bz2";
  22. publicstaticfinalintMSG_DWPACKSIZE=1;
  23. publicstaticfinalintMSG_DWSIZE=2;
  24. handleMessage()*/
  25. Handlerhandler=newHandler(){
  26. publicvoidhandleMessage(Messagemsg){
  27. //bar.incrementProgressBy(5);
  28. switch(msg.what){
  29. caseMSG_DWPACKSIZE:
  30. Stringsize=String.valueOf(msg.arg1);
  31. tvdwinfo.setText(size);
  32. Log.d("handleMessage","dwpacksize="+size);
  33. break;
  34. caseMSG_DWSIZE:
  35. Stringdwsize=String.valueOf(msg.arg1);
  36. tvdwsize.setText(dwsize);
  37. Log.d("handleMessage","dwsize="+dwsize);
  38. break;
  39. default:
  40. break;
  41. }
  42. }
  43. };
  44. @Override
  45. publicvoidonCreate(BundlesavedInstanceState){
  46. super.onCreate(savedInstanceState);
  47. setContentView(R.layout.main);
  48. tvdwsize=(TextView)findViewById(R.id.tvsize);
  49. tvdwinfo=(TextView)findViewById(R.id.tvinfo);
  50. mgr=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
  51. }
  52. publicvoidstartDownload(Viewv){
  53. Uriuri=Uri.parse(strUrl);
  54. Environment
  55. .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
  56. .mkdirs();
  57. Requestdwreq=newDownloadManager.Request(uri);
  58. dwreq.setTitle("Demo");
  59. dwreq.setDescription("android-ndk-r6-linux-x86.tar.bz2");
  60. dwreq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"android-ndk-r6-linux-x86.tar.bz2");
  61. dwreq.setNotificationVisibility(0);
  62. dwreq.setShowRunningNotification(true);
  63. lastDownload=mgr.enqueue(dwreq);
  64. }
  65. publicvoidqueryDownloadStatus(Viewv){
  66. RunnablequeryRunable=newRunnable(){
  67. longtotalsize=0;
  68. longdowsize=0;
  69. booleandownok=false;
  70. Cursorc=null;
  71. publicvoidrun(){
  72. //查询下载文件总大小
  73. totalsize=c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
  74. Messagemsg_packsize=newMessage();
  75. msg_packsize.what=MSG_DWPACKSIZE;
  76. msg_packsize.arg1=(int)totalsize;
  77. handler.sendMessage(msg_packsize);
  78. while(downok==false){
  79. c=mgr.query(newDownloadManager.Query().setFilterById(lastDownload));
  80. if(c==null){
  81. //tvdwsize.setText("query=null");
  82. }
  83. else{
  84. c.moveToFirst();
  85. //查询已经下载的大小
  86. dowsize=c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
  87. if(totalsize==dowsize)downok=true;
  88. }
  89. Messagemsg=newMessage();
  90. msg.what=MSG_DWSIZE;
  91. msg.arg1=(int)dowsize;
  92. handler.sendMessage(msg);
  93. try{
  94. Thread.sleep(5000);
  95. }catch(InterruptedExceptione){
  96. //TODOAuto-generatedcatchblock
  97. e.printStackTrace();
  98. }
  99. c.close();
  100. }
  101. }//run
  102. };
  103. Threadbackground=newThread(queryRunable);
  104. background.start();
  105. }
  106. publicvoiddelDownloads(Viewview){
  107. Toast.makeText(this,"delDownloads",Toast.LENGTH_LONG).show();
  108. mgr.remove(lastDownload);
  109. }

//查看下载窗口

  1. publicvoidviewDownWindow(Viewv){
  2. startActivity(newIntent(DownloadManager.ACTION_VIEW_DOWNLOADS));
  3. }
  4. }

更多相关文章

  1. android改变全局字体大小
  2. android页面用jquery窗口大小获取错误问题的解决
  3. Android中计算text文字大小的几个方法
  4. Android的DialogFragment的基本使用方法
  5. Android ImageView图片自适应大小
  6. SpannableString的使用方法
  7. android图片缩放(指定大小)

随机推荐

  1. android之Switch开关
  2. Android之解决在scrollview中嵌套ListVie
  3. Android 使用ViewDragHelper实现向slidin
  4. android IO流 写入 读出
  5. 如何定制化Android的播放器(VideoViewEx.
  6. Android 日期工具类DateUtil
  7. Android 调用系统相机回调后的处理
  8. Android 4.0 UI for Tablet and Handset
  9. Android系统进程Zygote启动过程的源代码
  10. android TV盒子开发遥控器按键的监听