Friday 13 July 2012

How to download and install .apk from server(not market app) programmatically

Hi folks,
If you are planning to develop android application which is responsible to download .apk file from server(not 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 are successfully downloaded and installed your .apk from server.
feel free to query @:    bpsingh216@gmail.com
enjoy coding :-)





20 comments:

  1. This site is truly helpful.. Thanks To Mr.BpSingh for creating such informative and helpful site for beginners in android app development.. Got a lot to learn!!

    ReplyDelete
  2. this is very good blog....really awesome content available here

    ReplyDelete
  3. please provide some material regarding task killer application

    ReplyDelete
  4. INSTALL_PACKAGES permission is only granted to system apps. How did you manage to do it? My IDE (Android Studio) won't even compile the application.

    ReplyDelete
    Replies
    1. press project-> Clean button and than code will be run fine

      Delete
  5. Why i am getting exception message " Parse error . There is a problem parsing the package"

    ReplyDelete
  6. checkUnknownSourceEnability() where is this method ?
    please provide this method

    ReplyDelete
  7. Is it possible to install application without apk being downloaded on the phone just like what googleplay playstore does...

    Thank you

    ReplyDelete
  8. When i try to run this i am getting my app has stopped.I am new to android. can you help me with this please

    ReplyDelete
  9. Hi man if you still Alive, i want to ask you ,where should i put these code (in OnCreate methode, or what ??), at least explain where to put them man .
    hope you answer or any one passes by

    ReplyDelete

  10. i got this exception at the time of installation;

    android.os.FileUriExposedException: file:///storage/emulated/0/test.apk exposed beyond app through Intent.getData()

    Can any one helm me.

    ReplyDelete

  11. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  12. Really very happy to say, your post is very interesting to read. I never stop myself to say something about it. You’re doing a great job. Keep it up…

    Upgrade your career Learn Oracle Training from industry experts gets complete hands on Training, Interview preparation, and Job Assistance at Softgen Infotech.

    ReplyDelete
  13. From your blog I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant blog. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.
    devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery

    ReplyDelete
  14. I like your such a valuable post for us. It is one of the excellent articles for this topic. It is very helpful for me and keeps doing...

    AWS training in Chennai

    AWS Online Training in Chennai

    AWS training in Bangalore

    AWS training in Hyderabad

    AWS training in Coimbatore

    AWS training

    ReplyDelete