1, first, the Android file download requires the following permissions:
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
2, because the file download is a long time task, can not be updated in the UI thread, need to open up a new thread to download the work, here we use the Android encapsulated asynchronous task Class--asynctask, this class to do some long-time work:
Private class Downloadapptask extends Asynctask<string, Integer, boolean> {
private int RESULT_OK = 200;
private int progress_id = 1;
Private context context;
Private Notificationmanager Manager;
Private Notificationcompat.builder Mbuilder;
private int size = 0;
private int Max;
Public Downloadapptask (Context context) {
This.context = context;
}
@Override
Protected Boolean doinbackground (String ... params) {
try {
Return DownloadURL (Params[0]);
} catch (IOException e) {
return false;
}
}
Private Boolean DownloadURL (String myurl) throws IOException {
InputStream is = null;
OutputStream OS = null;
URL url = new URL (myurl);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setreadtimeout (10000);
Conn.setconnecttimeout (15000);
Conn.setrequestmethod ("GET");
Conn.setdoinput (TRUE);
Conn.connect ();
int code = Conn.getresponsecode ();
url = null;
if (CODE==RESULT_OK) {
max = 0;
is = Conn.getinputstream ();
File File = new file (Environment.getexternalstoragedirectory (), "/app");
if (!file.exists ()) {
File.mkdir ();
}
Final file F = new file (File.getabsolutepath (), "myapp.apk");
OS = new FileOutputStream (f);
byte[] buffer = new byte[1000];
max = Conn.getcontentlength ();
New Thread (New Runnable () {
@Override
public void Run () {
while (max>size) {
try {
publishprogress (size);
Thread.Sleep (1000);
} catch (Interruptedexception e) {
}
}
Publishprogress (max);
Installapp (f);
}
}). Start ();
int Len;
while (len = is.read (buffer))!=-1) {
Os.write (Buffer,0,len);
size = size + len;
}
Os.flush ();
Os.close ();
Is.close ();
conn = null;
FILE = null;
return true;
}
return false;
}
@Override
protected void OnPreExecute () {
Manager = (Notificationmanager) getsystemservice (Context.notification_service);
Mbuilder = new Notificationcompat.builder (context);
Mbuilder.setcontenttitle ("app file Download")
. Setcontenttext ("In app file ...")
. Setsmallicon (R.drawable.ic_launcher);
}
@Override
protected void Onprogressupdate (Integer ... values) {
if (Max<=values[0]) {
Mbuilder.setcontenttext ("app file download complete!");
Mbuilder.setprogress (0, 0, false);
}else {
Mbuilder.setprogress (Max, values[0], false);
}
Manager.notify (Progress_id,mbuilder.build ());
}
File Download Complete Install app
private void Installapp (file file) {
Intent Intent = new Intent (Intent.action_view);
Intent.setdataandtype (uri.fromfile (file),
"Application/vnd.android.package-archive");
StartActivity (Intent);
}
}
3. Call the method in the main thread:
Determine if the network is available and then perform the download
private void Myclickhandler () {
Connectivitymanager manager = (Connectivitymanager) getsystemservice (Context.connectivity_service);
Networkinfo networkinfo = Manager.getactivenetworkinfo ();
if (networkinfo!=null&&networkinfo.isconnected ()) {
Downloadapptask task = new Downloadapptask (this);
Task.execute ("http://www.abc.2015/01/31/20150131140339351%E5%8D%83%E5%8D%92%E4%BF%9D%E8%BD%A6.apk");
}else {
Toast.maketext (This, "No network available!", Toast.length_long). Show ();
}
}
Android Learning Note 19: Android download file with progress bar with notification bar