downloads and installs the latest version of our App : Version « Hardware « Android






downloads and installs the latest version of our App

 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;
/**
 * @author Maurya
 */
class Main {
  /**
   * update() downloads and installs the latest version of our App. source:
   * http://stackoverflow.com/questions/4967669/android-install-apk-
   * programmatically
   */
  public static void update(Activity activity) {
    String apkurl = "http://50.56.69.127/vas/apk/911-Android.apk";
    String outputFileName = "911-Android.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/";
      File file = new File(PATH);
      file.mkdirs();
      File outputFile = new File(file, outputFileName);
      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();// .apk is download to sdcard in download file

      // install the .apk
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setDataAndType(Uri.fromFile(new File(Environment
          .getExternalStorageDirectory()
          + "/download/"
          + outputFileName)),
          "application/vnd.android.package-archive");
      activity.startActivity(intent);
    } catch (IOException e) {
      Toast.makeText(activity.getApplicationContext(), "Update error!",
          Toast.LENGTH_LONG).show();
    }
  }
}

   
  








Related examples in the same category

1.Honeycomb version
2.Check Android build version
3.Get version information
4.Using VersionedGestureDetector
5.Get App Version Name
6.Get App Version Code
7.Get Version Name with PackageManager
8.Get Package Version Code and Name
9.Android SDK version
10.Check for intallation, app name, app version, app version code
11.Get version number
12.Utility class providing array to int/long conversion for data received from devices through adb.
13.get Version Name
14.get Version Code
15.Byte value conversion