Install the latest apk without opening other programs.

Source: Internet
Author: User
Tags file url

Original Problem description: I checked the application version every 24 hours on the web server background. If the update is available, the system prompts you to download the new apk. [Java] Uri uri = Uri. parse (downloadURL); Intent intent = new Intent (Intent. ACTION_VIEW, uri); startActivity (intent); the above Code opens the user's browser and starts downloading. I don't want to open the browser, but I need to download the apk file. Or directly install the latest apk without opening other programs. How to implement it? Solution: First, you need to download the apk file String extStorageDirectory = Environment. getExternalStorageDirectory (). toString (); File folder = new File (extStorageDirectory, "APPS"); folder. mkdir (); File file = new File (folder, "AnyName. "+" apk "); try {file. createNewFile ();} catch (IOException e1) {e1.printStackTrace ();}/*** APKURL is your apk file url (server url) */DownloadFile ("APKURL ", file); The DownloadFile function is [java public void DownloadFile (String fileURL, File directory) {try {FileOutputStream f = new FileOutputStream (directory); URL u = new URL (fileURL ); httpURLConnection c = (HttpURLConnection) u. openConnection (); c. setRequestMethod ("GET"); // c. setDoOutput (true); c. connect (); InputStream in = c. getInputStream (); byte [] buffer = new byte [1024]; int len1 = 0; while (len1 = in. read (buffer)> 0) {f. write (buffer, 0, len1);} f. close ();} catch (Exception e) {System. out. println ("exception in DownloadFile: --------" + e. toString (); e. printStackTrace ();} after downloading the apk file, add the following code [java] Intent intent = new Intent (Intent. ACTION_VIEW); intent. setDataAndType (Uri. fromFile (new File (Environment. getExternalStorageDirectory () + "/APPS/" + "AnyName.apk"), "application/vnd. android. package-archive "); startActivity (intent); Add the permission to the manifest file [java] <uses-permission android: name =" android. permission. INTERNET "/> <uses-permission android: name =" android. permission. WRITE_EXTERNAL_STORAGE "/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.