【问题标题】:How to download and install an apk update in android studio remotly?如何在 android studio 中远程下载和安装 apk 更新?
【发布时间】:2022-07-01 14:48:06
【问题描述】:

如何在 Android 编程中下载和安装 apk 文件。我需要从包含我的 apk 文件的 URL 下载并安装新更新...通常的过程是检查是否有新版本,然后下载并安装它..

这是我现在使用的代码:

    val extStorageDirectory = Environment.getExternalStorageDirectory().toString()
        val folder = File(extStorageDirectory)
        Log.e("extStorageDirectory", extStorageDirectory)
        folder.mkdir()
        val file = File(folder, "AppName." + "apk")
        try {
            file.createNewFile()
            /**
             * APKURL is your apk file url(server url)
             */
            DownloadFile(apkLink, file)
        } catch (e1: IOException) {
//            e1.printStackTrace()
            Log.e("e1", e1.toString())
        }


    private fun DownloadFile(fileURL: String, directory: File) {

        try {
            val f = FileOutputStream(directory)
            val u = URL(fileURL)
            val c = u.openConnection() as HttpURLConnection
            c.requestMethod = "GET"
            //c.setDoOutput(true);
            c.connect()
            val `in` = c.inputStream
            val buffer = ByteArray(1024)
            var len1 = 0
            while (`in`.read(buffer).also { len1 = it } > 0) {
                f.write(buffer, 0, len1)
            }
            f.close()

            val intent = Intent(Intent.ACTION_VIEW)
            intent.setDataAndType(
                Uri.fromFile(
                    File(
                        Environment.getExternalStorageDirectory()
                            .toString()  + "AppName.apk"
                    )
                ), "application/vnd.android.package-archive"
            )
            startActivity(intent)

        } catch (e: Exception) {
            println("exception in DownloadFile: --------$e")
            e.printStackTrace()
            Log.e("exception in DownloadFile", e.toString())
        }
    }

我收到此错误:java.io.IOException: Operation not allowed

【问题讨论】:

    标签: java android kotlin apk auto-update


    【解决方案1】:

    在您的设备中转到设置-> 安全-> 未知来源-> 允许安装来自未知来源的应用程序。 因为你没有使用 CHplay,所以你必须接受来自其他来源的 apk

    【讨论】:

      猜你喜欢
      • 2017-03-28
      • 2016-03-25
      • 2020-08-06
      • 1970-01-01
      • 2019-09-07
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多