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
// 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: // 127.0.0.1: 8080/tomServer/file/db.zip "));
DmReq. setTitle ("db.zip"); // 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
Environment. DIRECTORY_DOWNLOADS
). 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,