Android app automatic updates

Android app automatic updates


Source code introduction

Automatic updates are possible even if the phone does not have an SD card. I have tested this myself.
Source code screenshot

Source code snippet

  1. int down_step = down_step_custom; // Prompt step  
  2. int totalSize; //Total file size  
  3. int downloadCount = 0 ; // The size of the downloaded files  
  4. int updateCount = 0 ; // The size of the uploaded file  
  5.           
  6. InputStream inputStream;
  7. OutputStream outputStream;
  8.   
  9. URL url = new URL(down_url);
  10. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  11. httpURLConnection.setConnectTimeout(TIMEOUT);
  12. httpURLConnection.setReadTimeout(TIMEOUT);
  13. // Get the size of the downloaded file  
  14. totalSize = httpURLConnection.getContentLength();
  15.           
  16. if (httpURLConnection.getResponseCode() == 404 ) {
  17. throw   new Exception( "fail!" );
  18. //This place should add a download failure process, but because we added a try---catch outside, the Exception has been handled.  
  19. //So no need to process  
  20. }
  21.           
  22. inputStream = httpURLConnection.getInputStream();
  23. outputStream = new FileOutputStream(file, false ); // Overwrite if the file exists  
  24.           
  25. byte buffer[] = new   byte [ 1024 ];
  26. int readsize = 0 ;
  27.           
  28. while ((readsize = inputStream.read(buffer)) != - 1 ) {
  29.               
  30. // /************If an error occurs during the download process, an error message will pop up and the notificationManager will be canceled*********/  
  31. // if (httpURLConnection.getResponseCode() == 404) {  
  32. // notificationManager.cancel(R.layout.notification_item);  
  33. // throw new Exception("fail!");  
  34. // //This place should add a download failure processing, but because we added a try---catch outside, the Exception has been handled.  
  35. // //So no need to process  
  36. // }  
  37.                           
  38. outputStream.write(buffer, 0 , readsize);
  39. downloadCount += readsize; // Get the downloaded size from time to time  
  40. /*** 3% increase each time**/  
  41. if (updateCount == 0 || (downloadCount * 100 / totalSize - down_step) >= updateCount) {
  42. updateCount += down_step;
  43. // Change the notification bar  
  44. contentView.setTextViewText(R.id.notificationPercent,updateCount + "%" );
  45. contentView.setProgressBar(R.id.notificationProgress, 100 ,updateCount, false );
  46. notification.contentView = contentView;
  47. notificationManager.notify(R.layout.notification_item, notification);
  48. }
  49. }
  50. if (httpURLConnection != null ) {
  51. httpURLConnection.disconnect();
  52. }
  53. inputStream.close();
  54. outputStream.close();
  55.           
  56. return downloadCount;
  57.      

Source code link: http://download..com/data/2012784

<<:  ActivityGroup: Jumping inside and outside of Activity

>>:  Android application source code imitating NetEase client source code effect

Recommend

Android Developer: Why I Switched to Kotlin

A word of caution, as a person who doesn't st...

What you need to know about adaptation - MobileWeb adaptation summary

Straight to the point, this article will summariz...

How to write efficient Android code

As Android-related devices are embedded devices, ...

Major changes in advertising and marketing strategies over the past century!

On the day JWT was merged with digital marketing ...

Why these plants? They are delicious and can fight zombies

Author: Riboapartment (Kunming Institute of Botan...

Alibaba's conspiracy and conspiracy to make up for the "Double 12"

The 50-yuan discount made offline stores crazy. T...