Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Android.app.AlertDialog;
Import Android.app.AlertDialog.Builder;
Import Android.app.Dialog;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnClickListener;
Import android.content.Intent;
Import Android.net.Uri;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.ProgressBar;
public class Updatemanager {
Private context Mcontext;
Private final String updatemsg = "Pro, there is a new version, to update it!"; Download Message hints
Private Dialog Noticedialog; Download prompt dialog box
Private Dialog Downloaddialog; Download Progress dialog box
Private ProgressBar Mprogressbar; Progress bar
Private Boolean Interceptflag = false; Mark whether the user cancelled the download during the download
Private Thread downloadapkthread = null; Download thread
Private final String Apkurl = "http://www.xxx/xxx.apk"; URL address for apk
Private final String Savepath = "/sdcard/updatedemo/"; Download the apk stored path
Private final String Savefilename = Savepath + "xxx.apk"; Download the apk file
private int progress = 0; Download progress
Private final int download_ing = 1; Tag is downloading
Private final int download_over = 2; Tag Download complete
Private final String tag= "version update";
Log Print Label
Private Handler Mhandler = new Handler () {//Update UI Handler
@Override
public void Handlemessage (msg) {
Super.handlemessage (msg);
Switch (msg.what) {
Case download_ing:
Update progress bar
Mprogressbar.setprogress (progress);
Break
Case Download_over:
Downloaddialog.dismiss ();
INSTALLAPK ();
Installation
Break
Default
Break
}
}
};
/*
* Construction Method
*/
Public Updatemanager {
This.mcontext = context;
}
/*
* Check if there is a need to update, compare version xml
*/
public void Checkupdate () {
Check to the server to see if the software has a new version
If there is
Shownoticedialog ();
}
/*
* Show version Update dialog box
*/
private void Shownoticedialog () {
Alertdialog.builder Builder = new Builder (mcontext);
Builder.settitle ("version update");
Builder.setmessage (UPDATEMSG);
Builder.setpositivebutton ("Update", new Onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
Noticedialog.dismiss ();
Showdownloaddialog ();
}
});
Builder.setnegativebutton ("later"), new Onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
Noticedialog.dismiss ();
}
});
Noticedialog = Builder.create ();
Noticedialog.show ();
}
/*
* Pop-up Download progress dialog box
*/
private void Showdownloaddialog () {
Alertdialog.builder Builder = new Builder (mcontext);
Builder.settitle ("Software Update");
Final Layoutinflater inflater = Layoutinflater.from (Mcontext);
View v = inflater.inflate (r.layout.progress, NULL);
Mprogressbar = (ProgressBar) V.findviewbyid (r.id.updateprogress);
Builder.setview (v);
Builder.setnegativebutton ("Cancel", new Onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
Downloaddialog.dismiss ();
Interceptflag = true;
}
});
Downloaddialog = Builder.create ();
Downloaddialog.show ();
DOWNLOADLATESTVERSIONAPK ();
}
/*
* Download the latest APK files
*/
private void downloadlatestversionapk () {
Downloadapkthread = new Thread (downloadapkrunnable);
Downloadapkthread.start ();
}
Anonymous inner class, apk file download thread
Private Runnable downloadapkrunnable = new Runnable () {
public void Run () {
try {
URL url = new URL (apkurl);
HttpURLConnection conn = (httpurlconnection) URL
. OpenConnection ();
Conn.setconnecttimeout (5*1000);
Conn.connect ();
int length = Conn.getcontentlength ();
LOG.E (TAG, "total number of bytes:" +length);
InputStream is = Conn.getinputstream ();
File File = new file (Savepath);
if (!file.exists ()) {
File.mkdir ();
}
File Apkfile = new file (savefilename);
FileOutputStream out = new FileOutputStream (apkfile);
int count = 0;
int readnum = 0;
byte[] buffer = new byte[1024];
do {
Readnum = is.read (buffer);
Count + = Readnum;
progress = (int) (((float) count/length) * 100);
LOG.E (TAG, "download Progress" +progress);
Mhandler.sendemptymessage (download_ing);
if (readnum <= 0) {
Download end
Mhandler.sendemptymessage (Download_over);
Break
}
Out.write (Buffer,0,readnum);
while (!interceptflag);
Is.close ();
Out.close ();
catch (Malformedurlexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
};
/*
* Installation of downloaded APK files
*/
private void installapk () {
File file= new file (Savefilename);
if (!file.exists ()) {
Return
}
Intent intent= New Intent (Intent.action_view);
Intent.setdataandtype (Uri.parse ("file://" +file.tostring ()), "application/vnd.android.package-archive");
Mcontext.startactivity (Intent);
}
}