简体   繁体   English

如何在android studio中远程下载和安装apk更新?

[英]How to download and install an apk update in android studio remotly?

How to download and install an apk file in Android programming.如何在 Android 编程中下载和安装 apk 文件。 I need to download and install new updates from a URL that contains my apk file...The usual procedure is to check if there's a new version then download it and install it..我需要从包含我的 apk 文件的 URL 下载并安装新更新...通常的过程是检查是否有新版本,然后下载并安装它。

This is the code that I'm using now:这是我现在使用的代码:

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

I'm getting this error: java.io.IOException: Operation not permitted我收到此错误:java.io.IOException: Operation not allowed

In your device go to Setting -> Security -> Unknow sources-> allow installation of apps from unknow source.在您的设备中,转到设置 -> 安全 -> 未知来源 -> 允许从未知来源安装应用程序。 Cause you are not use CHplay then you must accept apk from other source因为您没有使用 CHplay,所以您必须接受来自其他来源的 apk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM