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