public class Mainactivity extends activity {
Private Button Download;
/* SD Card root directory * *
Private File Rootdie;
/* Output File name * *
Private String outfilename = "Gua hu apk";
/* Progress Bar dialog Box * *
Private ProgressDialog Pdialog;
Private Myloadasynctask task;
Private Boolean falg=false;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Checkandcreatedir ();
Pdialog = new ProgressDialog (this);
/* Progress bar dialog Box Property set * *
Pdialog.setmessage ("Downloading ...");
Pdialog.settitle ("hint Information");
/* The maximum progress value of 100 * *
Pdialog.setmax (100);
/* Horizontal Style progress bar * *
Pdialog.setprogressstyle (progressdialog.style_horizontal);
/* Infinite Cycle mode * *
Pdialog.setindeterminate (FALSE);
/* Can cancel */
Pdialog.setcancelable (FALSE);
Pdialog.setbutton ("Paused", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
To delete a message queue
Handler.removemessages (PRO);
}
});
Pdialog.setbutton2 ("Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
if (Task!= null && task.getstatus () = = AsyncTask.Status.RUNNING) {
Falg=true;
Task.cancel (TRUE);
File File=new file (Rootdie + "/downloads/", outfilename);
if (file.exists ()) {
File.delete ();
}
}
}
});
Download= (Button) This.findviewbyid (r.id.download);
Download.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View arg0) {
/* Asynchronous Download/*
task= new Myloadasynctask ();
Task.execute ("Http://ttl6.pc6.com/lff/NetbankClient.zip");
}
});
}
/* Check sdcard and create catalog file * *
private void Checkandcreatedir () {
/* Get sdcard Directory * *
Rootdie = Environment.getexternalstoragedirectory ();
/* Directory of new files * *
File NewFile = new file (Rootdie + "/downloads/");
if (!newfile.exists ()) {
/* Create directory If file does not exist * *
Newfile.mkdirs ();
}
}
/* Asynchronous tasks, background processing and update UI * *
Class Myloadasynctask extends Asynctask<string, Integer, string> {
/* Background Thread * *
@Override
Protected string Doinbackground (String ... params) {
/* The URL of the downloaded file/*
try {
URL url = new URL (params[0]);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
URL property settings
Conn.setrequestmethod ("get");
URL to establish a connection
Conn.connect ();
/* Download the size of the file * *
int fileoflength = Conn.getcontentlength ();
Toast.maketext (Mainactivity.this, "fileoflength==" +fileoflength, 1). Show ();
/* The size of each download and the size of the total download * *
int totallength = 0;
int length = 0;
/* Input Stream * *
InputStream in = Conn.getinputstream ();
/* Output Flow * *
FileOutputStream out = new FileOutputStream (new File (Rootdie + "/downloads/", Outfilename));
/* Cache mode, download file/*
byte[] Buff = new byte[1024 * 1024];
while (length = In.read (buff)) > 0) {
if (iscancelled ()) return null;
totallength = length;
int str1 = (int) (double) totallength/fileoflength * 100);//calculated as a percentage before converting to integral type
Publishprogress (str1,totallength,fileoflength);
Out.write (buff, 0, length);
}
/* Close the input/output stream * *
In.close ();
Out.flush ();
Out.close ();
catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return null;
}
/* Preprocessing UI thread/*
@Override
protected void OnPreExecute () {
Super.onpreexecute ();
/* Display dialog box * *
Pdialog.show ();
}
/* End of UI thread/*
@Override
protected void OnPostExecute (String result) {
Super.onpostexecute (result);
Pdialog.dismiss ();
}
/* Process UI thread, which is invoked multiple times, triggering event as publicprogress method * *
@Override
protected void Onprogressupdate (Integer ... values) {
if (iscancelled ()) return;
Pdialog.setmessage (String.Format ("%d m,%d M",//byte is unit)
VALUES[1]/1024/1024, values[2]/1024/1024);//Assign values[1 to first%d, second to same
/* Progress Display * *
Pdialog.setprogress (Values[0]);
}
}
}
SOURCE Download Address: http://download.csdn.net/detail/sinat_28238111/9750735