歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android DownloadManager 使用

Android DownloadManager 使用

日期:2017/3/1 10:36:19   编辑:Linux編程
AAndroid 3.2 加入了DownloadManager ,這裡舉例使用方法。

layout添加兩個個button,兩個txtview

  1. <?xml version="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="Start Download"
  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="Query Status"
  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="del download"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:onClick="delDownloads"
  28. />
  29. <TextView android:id="@+id/tvsize"
  30. android:text="file"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content" />
  33. <TextView android:id="@+id/tvinfo"
  34. android:text="file"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content" />
  37. </LinearLayout>

  1. package com.download;
  2. import android.app.Activity;
  3. import android.app.DownloadManager;
  4. import android.app.DownloadManager.Request;
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.os.Environment;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. public class DWManagerThread extends Activity {
  17. private DownloadManager mgr=null;
  18. private long lastDownload=-1L;
  19. TextView tvdwsize ;
  20. TextView tvdwinfo=null;
  21. private String strUrl="http://dl.google.com/android/ndk/android-ndk-r6-linux-x86.tar.bz2";
  22. public static final int MSG_DWPACKSIZE=1;
  23. public static final int MSG_DWSIZE=2;
  24. handleMessage()*/
  25. Handler handler= new Handler(){
  26. public void handleMessage (Message msg) {
  27. // bar.incrementProgressBy(5);
  28. switch(msg.what){
  29. case MSG_DWPACKSIZE:
  30. String size=String.valueOf(msg.arg1);
  31. tvdwinfo.setText(size);
  32. Log.d("handleMessage", "dwpacksize="+size);
  33. break;
  34. case MSG_DWSIZE:
  35. String dwsize=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. public void onCreate(Bundle savedInstanceState) {
  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. public void startDownload(View v) {
  53. Uri uri=Uri.parse(strUrl);
  54. Environment
  55. .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
  56. .mkdirs();
  57. Request dwreq=new DownloadManager.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. public void queryDownloadStatus(View v) {
  66. Runnable queryRunable = new Runnable() {
  67. long totalsize=0;
  68. long dowsize=0;
  69. boolean downok=false;
  70. Cursor c=null;
  71. public void run() {
  72. //查詢下載文件總大小
  73. totalsize=c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
  74. Message msg_packsize=new Message();
  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(new DownloadManager.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. Message msg=new Message();
  90. msg.what=MSG_DWSIZE;
  91. msg.arg1=(int) dowsize;
  92. handler.sendMessage (msg);
  93. try {
  94. Thread.sleep(5000);
  95. } catch (InterruptedException e) {
  96. // TODO Auto-generated catch block
  97. e.printStackTrace();
  98. }
  99. c.close();
  100. }
  101. }//run
  102. };
  103. Thread background = new Thread(queryRunable);
  104. background.start();
  105. }
  106. public void delDownloads(View view) {
  107. Toast.makeText(this, "delDownloads", Toast.LENGTH_LONG).show();
  108. mgr.remove(lastDownload);
  109. }

//查看下載窗口

  1. public void viewDownWindow(View v) {
  2. startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
  3. }
  4. }
Copyright © Linux教程網 All Rights Reserved