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

Analysis of the elements and channels of online promotion plans!

Today we are going to talk about "How to cho...

What was Qin Shi Huang's name? Ying Zheng? Wrong!

Excerpted from: "Inside and Outside the Clas...

Christmas is coming, how to write copy that takes advantage of the situation?

When it comes to copywriting that leverages hot t...

Do you have promotional abilities?

There are many tasks that operations are responsi...

The most complete analysis of brand advertising in Tik Tok’s information flow!

Douyin ’s brand advertising product matrix: Douyi...

Try these tips to make the iPhone's native map app more convenient

Apple provides a variety of built-in apps for iOS...

YunOS adds power to the A380 with outstanding highlights

The emergence and popularization of smartphones h...

Don't be a "Parkinson", let's talk about Parkinson's

Parkinson's disease (PD) is not new to everyo...