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 :-)





please give me a complete solution, I implement this code but it is not working for me.
ReplyDeletethanx in advance.
This comment has been removed by the author.
ReplyDeleteINSTALL_PACKAGES
ReplyDeletethis perrmission can be given only to system applications. And is not required to install applications with user prompt.
Why i am getting exception message " Parse error . There is a problem parsing the package" ?
ReplyDeleteDont use signed apk use debug apk for both version you will not get this Error
Deleteeven unsigned apk also not parsing
DeleteThis comment has been removed by the author.
ReplyDeleteAPK is a file format for installing games, applications on devices using the Android operating system and if you want to download these Android apps and games to your computer, just install APK Downloader Online
ReplyDeleteAPK Downloader Online: The application supports downloading APK files to your computer.
where u write this method downloadApk()
ReplyDelete