Android control-DowloadManager

Source: Internet
Author: 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 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
// This method is a variable parameter and can have multiple parameters
Dm. remove (downloadId); // Delete
// Obtain the system DownloadManager.
Dm = (DownloadManager) getSystemService (DOWNLOAD_SERVICE );

Public void doClick (View v)
DownloadManager. Request dmReq =
// Convert to Uri format
New DownloadManager. Request (Uri. parse ("http: // 8080/tomServer/file/ "));
DmReq. setTitle (""); // set the title
DmReq. setDescription ("downloading! "); // Set the working status

// Disable sending a notification, that is, downloading down. setShowRunningNotification (false) in the background );

// The download interface down. setVisibleInDownloadsUi (false) is not displayed );



// Set the download method (3G and WIFI are set here)

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

// DmReqi. setDestinationUri integrates the following two methods:

// DmReq. setDestinationInExternalFilesDir (context, dirType, subPath); // set the location of the downloaded file

// DmReq. setDestinationInExternalPublicDir (dirType, subPath); // public path



DmReqi. setDestinationUri (
Uri. fromFile (new File (
// Set the public path
Environment. getExternalStoragePublicDirectory (
// Set the file
). GetAbsoluteFile ()
+ ". Zip ")
// Put it in a queue, and the system will give an Id in the queue;
DownloadId = dm. enqueue (dmReq );
// Set the referer. You can use the system to add multiple Referer and add multiple referer.
IntentFilter filter = new IntentFilter (DownloadManager. ACTION_DOWNLOAD_COMPLETE );
// Register Broadcast
RegisterReceiver (thereReceiver, filter );

TV. setText (TV. getText (). toString () + "download started: id =" + downloadId );

@ Override
Protected void onPause (){
// TODO Auto-generated method stub
Super. onPause ();
// Cancel registration Broadcast
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 );

Configuration file for android settings

Add the following two Permissions

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

In versions earlier than 2.2, the DownloadManager permission may also be added,



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

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.