android - 从我的网站在 Android 应用程序中安装更新

标签 android

我目前每 24 小时在我的网络服务器中检查一次应用程序版本,而不是在 android 市场中。如果更新可用,它会提示用户下载新的 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);

//下载文件函数是

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

//并在 list 中授予权限

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

它可能对你有帮助,我用它来满足你的需要。

关于android - 从我的网站在 Android 应用程序中安装更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14352839/

相关文章:

c# - 将后退按钮导航到另一个 Activity Xamarin C#

java - 带有 viewpager 和 picasso 的应用程序不断停止

Android : How to get Coordinates X, 缩放和平移后图像的 Y 值

Android ListView 选中项高亮显示

android - 与蓝牙设备配对与连接到蓝牙设备有什么区别?

android - 如何在 linux ubuntu 16.04 上安装 SDK 管理器?

java - 启动后停用闪屏

android - 在内部存储中保存公共(public)文件

android - Facebook Audience Network - 带有 AdListener 的 NativeAdsManager

android - 如何将基线对齐到多行 TextView 的底线?