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 weird can looking in the mirror get?

© Hardcore Gaming 101 Leviathan Press: "I am...

Google AdWords detailed tutorial from account registration to advertising

If you want to advertise on your own cross-border...

Jidu and Hesai Technology reached a strategic cooperation

Jidu is a technology startup dedicated to creatin...

How the Green Giant of Carbon Storage Can Resist an Unstable Climate System

Recently, my country has been affected by strong ...

Huawei App Market Brand Resource Bidding Promotion Service Rules!

These rules are based on the Huawei Developer Ser...

Is ibuprofen addictive? Are you taking ibuprofen correctly for menstrual cramps?

Author: Zhai Yan, Chief Physician, Department of ...