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

Is it easier to gain weight if you eat while walking?

In the fast-paced modern life, eating on the go s...

Rags and sweaters alert! Are humans powerless against moths that eat sweaters?

Leviathan Press: It’s hard to have a favorable op...

Why is App promotion becoming increasingly difficult?

Why? Is promotion becoming increasingly difficult...

Apple releases major macOS update: Night Shift mode added

At the same time as the official release of iOS 10...

Why are so many people waiting for the new iPad Pro?

April is almost a third of the way through, and t...

Guangdiantong Delivery Case V2.0 Design Summary (Interaction)

Project Background Before the revision of Guangdi...