Hi folks,
If you are planning to develop android application which is responsible to download .apk file from server(non market app,Enterprise distribution) and then install it on to the device ,this post will help you
so here is the choice.
1. Download Manager: this can be done using the Download Manager available from 2.3.3 your newely downloaded file goes in Downloads application as shown below,you can install your newly download .apk by clicking on it .
Android system just ask for permission to install your .apk with the help of Package Installer . This seems that after downloading their is no controls of your application to install it rather than going to downloads and manually install the .apk .I am not explaing the the code for Download Manager coz this post is all about "How to download .apk from server and install it programatically" :-).
So here is the 2nd way
2.
a. First download .apk file using simple http connection and store it on external storage area.
private void downloadapk(){
try {
URL url = new URL("http://www.efimoto.com/nilesh/HelloWorldProject.apk");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "filename.apk");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
this.checkUnknownSourceEnability();
this.initiateInstallation();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
b. Now instantiate installing process.
private void installApk(){
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File("/sdcard/filename.apk"));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}
c. Give uses-permission in application menifest file.
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.INTERNET"/>
d. Ensure that your "Unknown source " check box must enable.
I have been created simple UI for this having a button when button clicked it downloads apk after that installation process appears . If unknown source is disable you must got "Install blocked"
That's all, you have successfully downloaded and installed your .apk from server.
feel free to query @: bpsingh@pmtsl.onmicrosoft.com
enjoy coding :-)





