歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android DownloadManager 使用

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.     /> 
  23.     <Button 
  24.         android:id="@+id/del" 
  25.         android:text="del download" 
  26.         android:layout_width="wrap_content" 
  27.         android:layout_height="wrap_content" 
  28.         android:onClick="delDownloads" 
  29.  
  30.     /> 
  31.   
  32.     <TextView  android:id="@+id/tvsize" 
  33.         android:text="file" 
  34.     android:layout_width="wrap_content" 
  35.     android:layout_height="wrap_content" />   
  36.  
  37.  <TextView  android:id="@+id/tvinfo" 
  38.         android:text="file" 
  39.     android:layout_width="wrap_content" 
  40.     android:layout_height="wrap_content" /> 
  41. </LinearLayout> 

 

  1. package com.download; 
  2.  
  3.  
  4. import android.app.Activity; 
  5. import android.app.DownloadManager; 
  6. import android.app.DownloadManager.Request; 
  7. import android.content.Intent; 
  8. import android.database.Cursor; 
  9. import android.net.Uri; 
  10. import android.os.Bundle; 
  11. import android.os.Environment; 
  12. import android.os.Handler; 
  13. import android.os.Message; 
  14. import android.util.Log; 
  15. import android.view.View; 
  16. import android.widget.TextView; 
  17. import android.widget.Toast; 
  18.  
  19. public class DWManagerThread extends Activity { 
  20.  
  21.     private DownloadManager mgr=null; 
  22.     private long lastDownload=-1L; 
  23.   
  24.         TextView tvdwsize ;  
  25.         TextView tvdwinfo=null; 
  26.  
  27.     private String strUrl="http://dl.google.com/android/ndk/android-ndk-r6-linux-x86.tar.bz2"; 
  28.     public static final int MSG_DWPACKSIZE=1; 
  29.     public static final int MSG_DWSIZE=2; 
  30.  
  31.   handleMessage()*/ 
  32.     Handler handler= new Handler(){ 
  33.     public void handleMessage (Message msg) { 
  34.        // bar.incrementProgressBy(5); 
  35.         switch(msg.what){ 
  36.                 case MSG_DWPACKSIZE: 
  37.                     String  size=String.valueOf(msg.arg1); 
  38.                       tvdwinfo.setText(size); 
  39.                      Log.d("handleMessage", "dwpacksize="+size); 
  40.                     break; 
  41.                 case MSG_DWSIZE: 
  42.                     String dwsize=String.valueOf(msg.arg1); 
  43.                     tvdwsize.setText(dwsize); 
  44.                      Log.d("handleMessage", "dwsize="+dwsize); 
  45.                     break; 
  46.                 default: 
  47.                     break; 
  48.         } 
  49.  
  50.    } 
  51. }; 
  52.  
  53.     @Override 
  54.     public void onCreate(Bundle savedInstanceState) { 
  55.         super.onCreate(savedInstanceState); 
  56.         setContentView(R.layout.main); 
  57.  
  58.  
  59.         tvdwsize=(TextView)findViewById(R.id.tvsize); 
  60.         tvdwinfo=(TextView)findViewById(R.id.tvinfo); 
  61.  
  62.         mgr=(DownloadManager)getSystemService(DOWNLOAD_SERVICE); 
  63.  
  64.     } 
  65.  
  66.  
  67.     public void startDownload(View v) { 
  68.         Uri uri=Uri.parse(strUrl); 
  69.  
  70.         Environment 
  71.             .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) 
  72.             .mkdirs(); 
  73.  
  74.         Request dwreq=new DownloadManager.Request(uri); 
  75.  
  76.         dwreq.setTitle("Demo"); 
  77.         dwreq.setDescription("android-ndk-r6-linux-x86.tar.bz2"); 
  78.         dwreq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"android-ndk-r6-linux-x86.tar.bz2"); 
  79.         dwreq.setNotificationVisibility(0);
  80.         dwreq.setShowRunningNotification(true); 
  81.  
  82.         lastDownload=mgr.enqueue(dwreq); 
  83.  
  84.  
  85.     } 
  86.  
  87.  
  88. public void queryDownloadStatus(View v)  { 
  89.  
  90.       Runnable queryRunable = new Runnable() { 
  91.           long totalsize=0; 
  92.           long dowsize=0; 
  93.           boolean downok=false; 
  94.           Cursor c=null; 
  95.  
  96.         public void run() { 
  97.  
  98.             //查詢下載文件總大小 
  99.             totalsize=c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); 
  100.  
  101.             Message msg_packsize=new Message(); 
  102.             msg_packsize.what=MSG_DWPACKSIZE; 
  103.             msg_packsize.arg1=(int) totalsize; 
  104.             handler.sendMessage (msg_packsize); 
  105.  
  106.             while(downok==false){ 
  107.                          c=mgr.query(new DownloadManager.Query().setFilterById(lastDownload)); 
  108.  
  109.                         if (c==null) { 
  110.                             //tvdwsize.setText("query=null"); 
  111.                         } 
  112.                         else { 
  113.                             c.moveToFirst(); 
  114.                             //查詢已經下載的大小 
  115.                             dowsize=c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); 
  116.                             if(totalsize==dowsize) downok=true; 
  117.                         } 
  118.  
  119.                         Message msg=new Message(); 
  120.                         msg.what=MSG_DWSIZE; 
  121.                         msg.arg1=(int) dowsize; 
  122.                         handler.sendMessage (msg); 
  123.  
  124.                         try { 
  125.                             Thread.sleep(5000); 
  126.                         } catch (InterruptedException e) { 
  127.                             // TODO Auto-generated catch block 
  128.                             e.printStackTrace(); 
  129.                         } 
  130.                         c.close(); 
  131.                 } 
  132.             }//run 
  133.         }; 
  134.  
  135.            Thread background = new Thread(queryRunable); 
  136.            background.start(); 
  137.  
  138.     public void delDownloads(View view) { 
  139.  
  140.         Toast.makeText(this, "delDownloads", Toast.LENGTH_LONG).show(); 
  141.         mgr.remove(lastDownload); 
  142.  
  143.  
  144.    } 

//查看下載窗口

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