Android apk automatic detection and upgrade
First, obtain the local apk version:
/**
* Obtain the local software version
*/
Public static int getLocalVersion (Context ctx ){
Int localVersion = 0;
Try {
PackageInfo packageInfo = ctx. getApplicationContext ()
. GetPackageManager (). getPackageInfo (ctx. getPackageName (), 0 );
LocalVersion = packageInfo. versionCode;
Log. d ("TAG", "version of the software .. "+ LocalVersion );
} Catch (NameNotFoundException e ){
E. printStackTrace ();
}
Return localVersion;
}
Then obtain the server version, which can be obtained by yourself in many ways.
Obtain the server version and compare it with the local apk version:
If the version is earlier than the server version, update it: (I wrote it for reference only. I downloaded it asynchronously, and then showed the progress through notification)
/*** Update the app version ** @ param ctx * context object * @ param url * address of the updated version */private static PendingIntent pendingIntent; public static void UpdateVersion (final Context ctx, String url) {createNotification (ctx); // create a file and read the app_namecreateFile (ctx. getResources (). getString (R. string. app_name); new AsyncTask
() {@ Overrideprotected String doInBackground (String... params) {int down_step = 1; // prompt stepint totalSize; // total file size int downloadCount = 0; // The downloaded size int updateCount = 0; // The size of the uploaded file: InputStream inputStream; OutputStream outputStream; URL url; HttpURLConnection httpURLConnection = null; try {url = new URL (params [0]); httpURLConnection = (HttpURLConnection) url. openConnection (); httpURLConnection. setConnectTim Eout (TIMEOUT); httpURLConnection. setReadTimeout (TIMEOUT); // obtain the sizetotalSize of the downloaded object = httpURLConnection. getContentLength (); // Log. d ("TAG", "totalSize" + totalSize); if (httpURLConnection. getResponseCode () = 404) {throw new Exception ("fail! ");} InputStream = httpURLConnection. getInputStream (); // File appFile = File. createTempFile ("zhongtuo iron and steel ",". apk "); outputStream = new FileOutputStream (updateFile, false); // if a file exists, it overwrites byte buffer [] = new byte [1024]; int readsize = 0; while (readsize = inputStream. read (buffer ))! =-1) {outputStream. write (buffer, 0, readsize); downloadCount + = readsize; // obtain the downloaded size from time to time // Log. d ("TAG", "downloadCount" + downloadCount ); /*** add 1% */if (updateCount = 0 | (downloadCount * 100/totalSize-down_step)> = updateCount) {updateCount + = down_step; // change the notification bar // Log. d ("TAG", "Start to download .. "); Builder = new Notification. builder (ctx ). setSmallIcon (R. drawable. logo ). setContentText ("downloading (" + updateCount + "% )... "). setProgress (100, updateCount, false); manager. notify (8, builder. build () ;}} if (httpURLConnection! = Null) {httpURLConnection. disconnect ();} inputStream. close (); outputStream. close ();} catch (MalformedURLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();} return null;} @ Overrideprotected void onPostExecute (String result) {// download complete, click to install System. out. println ("updateFile .. "+ updateFile); Uri uri = Uri. fromFile (updateFile); Intent intent = new Intent (Intent. ACTION_VIEW); intent. setDataAndType (uri, "application/vnd. android. package-archive "); pendingIntent = PendingIntent. getActivity (ctx, 0, intent, 0); builder = new Notification. builder (ctx ). setSmallIcon (R. drawable. logo ). setContentIntent (pendingIntent ). setContentTitle ("apk download and update "). setContentText ("Download succeeded, please click to install "). setProgress (100,100, false); manager. notify (8, builder.build());}.exe cute (url);} private static void createNotification (Context ctx) {// The most common notification bar manager = (icationicationmanager) ctx. getSystemService (ctx. NOTIFICATION_SERVICE); builder = new Notification. builder (ctx ). setSmallIcon (R. drawable. logo ). setContentTitle ("apk download and update "). setContentText ("prepare to download... "). setProgress (100, 0, false); manager. notify (8, builder. build ());}
After the download is complete, click Install.
Note: When your apk is signed, if the apk on your mobile phone is not signed, the system will prompt that the packages with different signatures conflict during installation and cannot be installed, in this way, only the unsigned apk can be uninstalled and then installed.