Android apk version update
Android apk version update
/**
* Get the version name
*
* @ Return version
*/
Private String getVersionName (){
Try {
// Obtain the packagemanager instance
PackageManager packageManager = getPackageManager ();
// GetPackageName () is the package name of your current class, and 0 indicates obtaining version information
PackageInfo packInfo = packageManager. getPackageInfo (
GetPackageName (), 0 );
Version = packInfo. versionName; // versionName is used to obtain the version name and version number...
} Catch (NameNotFoundException e ){
E. printStackTrace ();
}
Return version;
}
Activity call
// Check whether the version needs to be updated.
UpdateManager mUpdateManager = new UpdateManager (
SettingActivity. this );
MUpdateManager. checkUpdateInfo ();
Public class UpdateManager {
Private Context mContext;
/* Download package installation path */
Private static final String savePath = "// sdcard // updatedemo //";
Private static final String saveFileName = savePath + "licaike.apk ";
/* Progress bar and handler and msg constants that notify the ui to refresh */
Private int progress;
Private ProgressDialogUtil pdu;
Private boolean interceptFlag = true;
@ SuppressLint ("HandlerLeak ")
Private Handler mHandler = new Handler (){
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case 0x123:
Pdu. setProgress (progress );
Break;
Case 0x124: // install apk
File apkfile = new File (saveFileName );
If (! Apkfile. exists ()){
Return;
}
Intent I = new Intent (Intent. ACTION_VIEW );
I. setDataAndType (Uri. parse ("file: //" + apkfile. toString ()),
"Application/vnd. android. package-archive ");
MContext. startActivity (I );
Pdu. dismiss ();
Break;
Default:
Break;
}
};
};
Public UpdateManager (Context context ){
This. mContext = context;
}
// External interface for the main Activity to call
Public void checkUpdateInfo (){
Final AlertDialogUtil dialogUtil = new AlertDialogUtil (mContext, false,
Null );
DialogUtil. setMessage ("Please update the new version ");
DialogUtil. setBtnPositiveValue ("Update ");
DialogUtil. setPositiveClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View arg0 ){
ShowDownloadDialog ();
DialogUtil. dismiss ();
}
});
DialogUtil. setBtnNegativeValue ("canceled ");
DialogUtil. setNegativeClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View arg0 ){
DialogUtil. dismiss ();
}
});
DialogUtil. show ();
}
Private void showDownloadDialog (){
// Download apk
DownLoadThread ();
Pdu = new ProgressDialogUtil (mContext, false, null );
Pdu. setMessage ("the software is being updated ...");
Pdu. setBtnNegativeValue ("canceled ");
Pdu. setNegativeClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View arg0 ){
InterceptFlag = false;
Pdu. dismiss ();
}
});
Pdu. show ();
}
Private void downLoadThread (){
New Thread (new Runnable (){
@ Override
Public void run (){
InputStream is = null;
FileOutputStream fos = null;
Try {
// Url of the returned installation package
String apkUrl = "http://d.m.hexun.com/app/licaike.apk ";
URL url = new URL (apkUrl );
HttpURLConnection conn = (HttpURLConnection) url
. OpenConnection ();
Conn. connect ();
Int length = conn. getContentLength ();
Is = conn. getInputStream ();
File file = new File (savePath );
If (! File. exists ()){
File. mkdir ();
}
String apkFile = saveFileName;
File ApkFile = new File (apkFile );
Fos = new FileOutputStream (ApkFile );
Int count = 0;
Byte buf [] = new byte [1024];
While (interceptFlag) {// click Cancel to stop the download.
Int numread = is. read (buf );
Count + = numread;
Progress = (int) (float) count/length) * 100 );
// Update Progress
MHandler. sendEmptyMessage (0x123 );
If (numread <= 0 ){
// Download completion notification Installation
MHandler. sendEmptyMessage (0x124 );
Break;
}
Fos. write (buf, 0, numread );
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
If (fos! = Null)
Fos. close ();
If (is! = Null)
Is. close ();
} Catch (Exception e ){
} Finally {
Fos = null;
Is = null;
}
}
}
}). Start ();
}
}
Drag the progress bar to the dialog box by yourself