android控制項之DowloadManager

來源:互聯網
上載者:User

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("http://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的使用許可權,

 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.