从website中安装android应用程序更新

我在web服务器的后台每24小时检查一次应用程序版本。
如果更新可用,它会提示用户下载新的apk。

Uri uri = Uri.parse(downloadURL);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);

上面的代码打开用户的浏览器,并开始下载。
我不想打开浏览器,但是需要下载apk文件文件。 或者直接安装最新的apk而不用打开其他的程序。如何实现呢?

首先,你需要下载apk文件

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

DownloadFile函数是

 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();
    }

下载apk文件后,添加下面的代码

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

在 manifest文件中添加权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>