Based on the online changes
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 = ", there is a new version. Do you want to update it! "; // Download message prompt
Private dialog noticedialog; // download prompt dialog box
Private dialog downloaddialog; // download progress dialog box
Private progressbar mprogressbar; // progress bar
Private Boolean interceptFlag = false; // indicates whether the user cancels the download.
Private Thread downloadApkThread = null; // download Thread
Private final String apkUrl = "http://www.xxx/xxx.apk"; // URL of the apk
Private final String savePath = "/sdcard/UpdateDemo/"; // path of the downloaded apk
Private final String saveFileName = savePath + "xxx.apk"; // The downloaded apk file.
Private int progress = 0; // download progress
Private final int DOWNLOAD_ING = 1; // The flag is being downloaded.
Private final int DOWNLOAD_OVER = 2; // indicates that the download is complete.
Private final String TAG = "version Update ";
// Log printing label
Private Handler mhandler = new Handler () {// update the UI handler
@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
Switch (msg. what ){
Case DOWNLOAD_ING:
// Update the progress bar
MProgressBar. setProgress (progress );
Break;
Case DOWNLOAD_OVER:
DownloadDialog. dismiss ();
InstallApk ();
// Install
Break;
Default:
Break;
}
}
};
/*
* Constructor
*/
Public UpdateManager (Context context ){
This. mContext = context;
}
/*
* Check whether updates are required. Compare the specific xml version.
*/
Public void checkUpdate (){
// Go to the server to check whether the software has a new version
// If yes
ShowNoticeDialog ();
}
/*
* The version update dialog box is displayed.
*/
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 ();
}
/*
* The download progress dialog box is displayed.
*/
Private void showdownloaddialog (){
Alertdialog. 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 File
*/
Private void downloadlatestversionapk (){
Downloadapkthread = new thread (downloadapkrunnable );
Downloadapkthread. Start ();
}
// Anonymous internal 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 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 ends
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 ();
}
}
};
/*
* Install the downloaded apk File
*/
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 );
}
}