Android apk version update

Source: Internet
Author: User

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

 

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.