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

How to make use of in-app advertising space?

As the production of apps increases year by year,...

Talk about the traffic monetization of self-media

I posted an advertisement two days ago about a ce...

How do you start to create an offline event?

1. To organize a successful offline event , we ne...

From the "glue dispensing door" to see Xiaomi's true "triathlon"

Let’s briefly review the so-called “glue dispensi...

Brand Matrix Building Guide

This article will talk about "How to build I...

5 steps to write a good B2B content marketing white paper from 0 to 1

Starting from the origin of white papers and comb...

Galaxy S6 Edge vs iPhone 6 drop resistance comparison: Apple is shocked

Galaxy S6 Edge is already on sale overseas, and f...

Source code | Practical calendar control

Source code introduction: A practical calendar co...

The "Sky Catcher" Builders on the 4,400-meter Plateau

How long does it take to walk 1.3 square kilomete...

The barbaric P2P online lending has seen the dawn of civilization

For the P2P industry, the impact of policies has ...