Android apk automatic detection and upgrade and androidapk Detection

Source: Internet
Author: User

Android apk automatic detection and upgrade and androidapk Detection

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 <String, Void, String> () {@ Overrideprotected String doInBackground (String... params) {int down_step = 1; // The stepi prompt is displayed. Nt 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. setConnectTimeout (TIMEOUT); httpURLConnection. setReadTimeout (TIMEOUT); // obtain the sizetotalSize of the downloaded object = httpURLCo Nnection. 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.







Can the android Resource Package (apk) be upgraded automatically?

Download a pea pod. After the data line is connected, your mobile phone model is automatically identified. You can also update the software on your mobile phone. There are thousands of software and other things. Very convenient. Hope to adopt it.

Where is the APK after the android app is updated? That is, the old APK will be automatically overwritten or deleted.

Yes.
Unless you use the Android marketplace software to download and set to save the source file, you will not be able to lose it.

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.