Android automatically updates the software version

Source: Internet
Author: User

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 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 );

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.