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