android控件之DowloadManager

package com.lan.www;

import java.io.File;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DowloadManagerActivity extends Activity {
    /** Called when the activity is first created. */
    TextView tv;
    Button btDel;
    DownloadManager dm;
    long downloadId;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv);
        btDel = (Button) findViewById(R.id.button2);
        btDel.setOnClickListener(new OnClickListener() {
           
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //這個方法是變參,可以有多個參數
                dm.remove(downloadId);//刪除
            }
        });
        //得到系統的DownloadManager
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
       
    public void doClick(View v)
    {
        DownloadManager.Request dmReq =
                    //轉化成Uri的格式
                new DownloadManager.Request(Uri.parse("https://127.0.0.1:8080/tomServer/file/db.zip"));
        dmReq.setTitle("db.zip");//設置標題
        dmReq.setDescription(" downloading!");//設置工作狀態
      

//禁止發出通知,既後臺下載  down.setShowRunningNotification(false); 

//不顯示下載界面   down.setVisibleInDownloadsUi(false); 

 

 

        //設置下載方式,(這裡設置的是3G和WIFI)

 dmReq.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI);

       //dmReqi.setDestinationUri整合瞭下面兩種方法

      //dmReq.setDestinationInExternalFilesDir(context, dirType, subPath);//設置下載後文件存放的位置

       // dmReq.setDestinationInExternalPublicDir(dirType, subPath); //公共路徑

 

 

        dmReqi.setDestinationUri(
                Uri.fromFile(new File(
                        //設置公共路徑
                        Environment.getExternalStoragePublicDirectory(
                                //設置文件
                                Environment.DIRECTORY_DOWNLOADS
                                ).getAbsoluteFile()
                        +".zip")
                ));
        //放到一個隊列裡,隊列裡系統裡會給一個Id;
        downloadId = dm.enqueue(dmReq);
        //設置過慮器,用系統給的就可以,可以添加多外過慮器,添加多個用add
        IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        //註冊廣播
        registerReceiver(thereReceiver, filter);
       
        tv.setText(tv.getText().toString()+" download started : id = "+downloadId);
    }
   
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        //解除註冊廣播
        unregisterReceiver(thereReceiver);
    }
   
    public BroadcastReceiver thereReceiver = new BroadcastReceiver() {
       
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Bundle extras = intent.getExtras();
            long doneId = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
            tv.setText(tv.getText().toString()+" \nfinish "+doneId);
        }
    };
}

android設置配置文件

添加下面兩條權限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

2.2以前版本的可能還要添加DownloadManager的使用權限,

 

 

You May Also Like