5

I got requirement to host my .apk file on one public site other than android market or any other app stores. In android market, after registred in to the market the downloded .apk will automatically installed on mobile without any manual action. So I am willing to create one URL and host my .apk file in to that and want to download that .apk in to the android mobile and it has to install automatically.

How can I do that....plz share if any code or links are there regrading this.

2 Answers 2

5

If Android device has Settings->Applications->Unknown Sources checked, Android will allow .apk installation. If its not checked - you will not succeed.

Assuming that check bar is checked and you have downloaded .apk file. You can run next code to trigger installation:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(apkFileName));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
1
  • 3
    i might be missing something simple; but how do i run this code (allowing me to get an application onto my device) so i can code onto my device?
    – Ian Boyd
    Dec 6, 2012 at 18:23
2
{
        String url = "http://www.server.com/yourapp.apk";
        String PATH = Environment.getExternalStorageDirectory() + "/download/";
        File file = new File(PATH);
        file.mkdirs();
        File outputFile = new File(file, "yourapp.apk");
        downloadFile(url, outputFile);
        installApp(context);
}



private static void downloadFile(String url, File outputFile) {
    try {
        URL u = new URL(url);
        URLConnection conn = u.openConnection();
        int contentLength = conn.getContentLength();

        DataInputStream stream = new DataInputStream(u.openStream());

        byte[] buffer = new byte[contentLength];
        stream.readFully(buffer);
        stream.close();

        DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
        fos.write(buffer);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("FileNotFoundException",e+"");
        return; 
    } catch (IOException e) {
        Log.e("IOException",e+"");
        return; 
    }
}


private static void installApp(Context mycontext) {
    Intent installer = new Intent();
    installer.setAction(android.content.Intent.ACTION_VIEW);
    String PATH = "file://" + Environment.getExternalStorageDirectory() + "/download/yourapp.apk";
    installer.setDataAndType(Uri.parse(PATH), "application/vnd.android.package-archive");
    mycontext.startActivity(installer);
}
1
  • getContentLength returns the content length in bytes specified by the response header field content-length or -1 if this field is not set. There is no guarantee that you will be provided with the correct value. Mar 26, 2012 at 19:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.