As follows:
The code is implemented as follows:
Package com.update.apk;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java.net. HttpURLConnection;
Import java.net. URL;
Import org. apache. http. HttpEntity;
Import org. apache. http. HttpResponse;
Import org. apache. http. client. HttpClient;
Import org. apache. http. client. methods. HttpGet;
Import org. apache. http. impl. client. DefaultHttpClient;
Import org. json. JSONArray;
Import org. json. JSONObject;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. Dialog;
Import android. app. ProgressDialog;
Import android. content. Context;
Import android. content. DialogInterface;
Import android. content. Intent;
Import android. content. pm. PackageManager. NameNotFoundException;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. OS. Handler;
Import android. OS. Message;
Import android. util. Log;
Public class MainActivity extends Activity {
/** Called when the activity is first created .*/
String newVerName = ""; // new version name
Int newVerCode =-1; // New Version Number
ProgressDialog pd = null;
String UPDATE_SERVERAPK = "ApkUpdateAndroid.apk ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
If (getServerVer ()){
Int verCode = this. getVerCode (this );
If (newVerCode> verCode ){
DoNewVersionUpdate (); // update the version
} Else {
NotNewVersionUpdate (); // The system prompts that the latest version is already in use.
}
}
}
/**
* Get the version number
*/
Public int getVerCode (Context context ){
Int verCode =-1;
Try {
VerCode = context. getPackageManager (). getPackageInfo ("com.update.apk", 0). versionCode;
} Catch (NameNotFoundException e ){
// TODO Auto-generated catch block
Log. e ("An error occurred while obtaining the version number", e. getMessage ());
}
Return verCode;
}
/**
* Obtain the version name.
*/
Public String getVerName (Context context ){
String verName = "";
Try {
VerName = context. getPackageManager (). getPackageInfo ("com.update.apk", 0). versionName;
} Catch (NameNotFoundException e ){
Log. e ("version name retrieval exception", e. getMessage ());
}
Return verName;
}
/**
* Obtain the version number and version name from the server.
* @ Return
*/
Public boolean getServerVer (){
Try {
URL url = new URL ("http: // 10.0.2.2: 8080/ApkUpdateService/ver ");
HttpURLConnection httpConnection = (HttpURLConnection) url. openConnection ();
HttpConnection. setDoInput (true );
HttpConnection. setDoOutput (true );
HttpConnection. setRequestMethod ("GET ");
HttpConnection. connect ();
InputStreamReader reader = new InputStreamReader (httpConnection. getInputStream ());
BufferedReader bReader = new BufferedReader (reader );
String json = bReader. readLine ();
JSONArray array = new JSONArray (json );
JSONObject jsonObj = array. getJSONObject (0 );
NewVerCode = Integer. parseInt (jsonObj. getString ("verCode "));
NewVerName = jsonObj. getString ("verName ");
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return false;
}
Return true;
}
/**
* Do not update the version.
*/
Public void notNewVersionUpdate (){
Int verCode = this. getVerCode (this );
String verName = this. getVerName (this );
StringBuffer sb = new StringBuffer ();
Sb. append ("current version :");
Sb. append (verName );
Sb. append ("Code :");
Sb. append (verCode );
Sb. append ("\ n is the latest version, no need to update ");
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("Software Update ")
. SetMessage (sb. toString ())
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
Finish ();
}
}). Create ();
Dialog. show ();
}
/**
* Update version
*/
Public void doNewVersionUpdate (){
Int verCode = this. getVerCode (this );
String verName = this. getVerName (this );
StringBuffer sb = new StringBuffer ();
Sb. append ("current version :");
Sb. append (verName );
Sb. append ("Code :");
Sb. append (verCode );
Sb. append (", found version :");
Sb. append (newVerName );
Sb. append ("Code :");
Sb. append (verCode );
Sb. append (", update ");
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("Software Update ")
. SetMessage (sb. toString ())
. SetPositiveButton ("Update", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
Pd = new ProgressDialog (MainActivity. this );
Pd. setTitle ("downloading ");
Pd. setMessage ("Please wait... ");
Pd. setProgressStyle (ProgressDialog. STYLE_SPINNER );
DownFile ("http: // 10.0.2.2: 8080/ApkUpdateService/ApkUpdateAndroid.apk ");
}
})
. SetNegativeButton ("not updated now", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
Finish ();
}
}). Create ();
// Display the update box
Dialog. show ();
}
/**
* Download apk
*/
Public void downFile (final String url ){
Pd. show ();
New Thread (){
Public void run (){
HttpClient client = new DefaultHttpClient ();
HttpGet = new HttpGet (url );
HttpResponse response;
Try {
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 (), UPDATE_SERVERAPK );
FileOutputStream = new FileOutputStream (file );
Byte [] B = new byte [1024];
Int charb =-1;
Int count = 0;
While (charb = is. read (B ))! =-1 ){
FileOutputStream. write (B, 0, charb );
Count + = charb;
}
}
FileOutputStream. flush ();
If (fileOutputStream! = Null ){
FileOutputStream. close ();
}
Down ();
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}. Start ();
}
Handler handler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
Pd. cancel ();
Update ();
}
};
/**
* The download is complete. The download dialog box is canceled using handler.
*/
Public void down (){
New Thread (){
Public void run (){
Message message = handler. obtainMessage ();
Handler. sendMessage (message );
}
}. Start ();
}
/**
* Install the application
*/
Public void update (){
Intent intent = new Intent (Intent. ACTION_VIEW );
Intent. setDataAndType (Uri. fromFile (new File (Environment. getExternalStorageDirectory (), UPDATE_SERVERAPK ))
, "Application/vnd. android. package-archive ");
StartActivity (intent );
}
}
From shmily (^ ω ^)