Created
          October 22, 2012 19:14 
        
      - 
      
- 
        Save aritzg/3933466 to your computer and use it in GitHub Desktop. 
    APK auto-update routines
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private boolean newVersionAvailable(){ | |
| Log.d(TAG, "Check for updates"); | |
| RestUtils.initRestTemplate(apkRestClient.getRestTemplate()); | |
| Log.d(TAG, "Remote version " + apkRestClient.getVersion()); | |
| return false; | |
| } | |
| private void update() { | |
| Log.d(TAG, "Updating apk"); | |
| String apkurl = "http://aaa/aaa.apk"; | |
| try { | |
| URL url = new URL(apkurl); | |
| HttpURLConnection c = (HttpURLConnection) url.openConnection(); | |
| c.setRequestMethod("GET"); | |
| c.setDoOutput(true); | |
| c.connect(); | |
| String PATH = Environment.getExternalStorageDirectory() | |
| + "/download/path"; | |
| File file = new File(PATH); | |
| file.mkdirs(); | |
| File outputFile = new File(file, "aaa.apk"); | |
| FileOutputStream fos = new FileOutputStream(outputFile); | |
| InputStream is = c.getInputStream(); | |
| byte[] buffer = new byte[1024]; | |
| int len1 = 0; | |
| while ((len1 = is.read(buffer)) != -1) { | |
| fos.write(buffer, 0, len1); | |
| } | |
| fos.close(); | |
| is.close();// till here, it works fine - .apk is download to my | |
| // sdcard in download file | |
| Intent promptInstall = new Intent(Intent.ACTION_VIEW); | |
| promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/path/" + "aaa.apk")), "application/vnd.android.package-archive"); | |
| //promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| startActivity(promptInstall);// installation is not working | |
| } catch (Exception e) { | |
| Log.e(TAG, "Update error!",e); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment