(Android practice) online application version update

Source: Internet
Author: User

Step One application system configuration file-application version configuration information

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "TP. NationalTest"
Android: versionCode = "4"
Android: versionName = "1.4" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "4"/>

VersionCode: Version Number

VersionName:Version name

MinSdkVersion: Minimum SDk version

Step Two backend verifies whether the current application version is to be updated

 

/* Check the application version */
Private void CheckAppEdtion ()
{
// Determine the network connection, such as the connection
// Step One checks whether there is a network connection,
If (! NetWorkUtility. CheckConnection (this ))
{
Return;
}
// Obtain the version information of the Network Platform
CAppInfo model = AppDataServiceHelper. GetAppEdtionInfo (ProjectConstant. AppID );

// Compare the version information. The new version dialog box is displayed.

If (model! = Null)
{

Try {
PackageInfo info = this. getPackageManager (). getPackageInfo (this. getPackageName (), 0 );
Int oldVersion = info. versionCode;
Int newVersion = Integer. parseInt (model. Edition );
If (newVersion> oldVersion ){
// Create an update dialog box.
AppUpdateDialog. CreateRegisterAlert (this );
}
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}


In the Step THree dialog box, select whether to update the APK.

Static ProgressDialog m_Dialog;
Static Context m_context = null;
Static AlertDialog appupdatedialog = null;
/* Create a single question dialog box */
Public static void CreateRegisterAlert (Context context)
{
M_context = context;
Dialog dialog = new AlertDialog. Builder (context). setTitle ("system update ")
. SetMessage ("new version already exists. Are you sure you want to update it? ")
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
M_Dialog = ProgressDialog. show (m_context, "Please wait...", "Updating... please wait...", true );
MRegsiterHandler. sleep (1000 );

}
})
. SetNegativeButton ("cancel", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){

}
}). Create ();
Dialog. show ();

}

 Step four: Download the APK and update the application version

View Code

/*
* Regular registration program
**/
Private static RegsiterHandler mRegsiterHandler = new RegsiterHandler ();

Static class RegsiterHandler extends Handler {

@ Override
Public void handleMessage (Message msg ){
Try {
String url = "http://xx.xx.xx.xx/gointel/nationaltest.html ";
HttpClient client = new DefaultHttpClient ();
HttpGet = new HttpGet (url );
HttpResponse response;

Response = client.exe cute (get );
HttpEntity entity = response. getEntity ();
Long length = entity. getContentLength ();
InputStream is = entity. getContent ();
FileOutputStream fileOutputStream = null;
If (is! = Null ){

File file = new File (Environment
. GetExternalStorageDirectory (), "nationaltest.apk ");
FileOutputStream = new FileOutputStream (file );


Byte [] buf = new byte [1, 1024];
Int ch =-1;
While (ch = is. read (buf ))! =-1 ){
FileOutputStream. write (buf, 0, ch );
}

}
FileOutputStream. flush ();
If (fileOutputStream! = Null ){
FileOutputStream. close ();
}

}
Catch (Exception ex)
{

}
Finally {
M_Dialog.cancel ();
}
Install ();

}
Public void sleep (long delayMillis ){
This. removeMessages (0 );
SendMessageDelayed (obtainMessage (0), delayMillis );
}
};


Private static void install (){
Intent I = new Intent ();
// I. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
I. setAction (Intent. ACTION_VIEW );
I. setDataAndType (Uri. fromFile (new File ("/sdcard/nationaltest.apk ")),
"Application/vnd. android. package-archive ");
M_context.startActivity (I );
// Finish ();
}

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.