【问题标题】:Downloading and installing an APK using volley使用 volley 下载和安装 APK
【发布时间】:2017-01-28 02:05:54
【问题描述】:

我正在尝试通过网络下载更新。这是最新的凌空版本。我目前使用的是Stringrequest

class UpdateService : IntentService("Update Serivce") {
companion object {
    val VERSION_URL = "<version url>"
    val FILE_URL = "<apk url>"
    val LOG_TAG = "UPDATE"
    fun initialize(context: Context) = context.startService(Intent(context, UpdateService::class.java))
}

/**
 * Checks remote for an update
 */
override fun onHandleIntent(intent: Intent?) {
    Log.d(LOG_TAG, "Retrieving new version")
    // Get version number
    NetworkService().get(Uri.parse(VERSION_URL),
            Response.Listener { success(it) },
            Response.ErrorListener {
                Log.d(LOG_TAG, "Failed to update ${it.toString()}")
                FirebaseCrash.report(it)
            })
}

private fun success(it: String?) {
    Log.d(LOG_TAG, "Remote version: $it")
    Log.d(LOG_TAG, "Local version: ${BuildConfig.VERSION_NAME}")

    if (!it.equals(BuildConfig.VERSION_NAME)) {
        Log.d(LOG_TAG, "Fetching new update")
        NetworkService().get(
                Uri.parse(FILE_URL),
                Response.Listener { installUpdate(it) },
                Response.ErrorListener {
                    Log.d(LOG_TAG, "failed to get new APK")
                    FirebaseCrash.report(it)
                }
        )
    }
}

private fun installUpdate(it: String) {
    Log.d(LOG_TAG, "retrieved update")

    val file = File(this.externalCacheDir, "update.apk")

    if(!file.exists()){
        file.createNewFile()
    }

    file.writeBytes(it.toByteArray())

    val intent = Intent(Intent.ACTION_VIEW)
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive")
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

    Log.d(LOG_TAG, "Installing")

    startActivity(intent)
}

}

启动安装活动时,我会收到来自 android 的解析错误。

如何通过 volley 下载文件并将其保存为可用的 APK?

【问题讨论】:

  • 检查apk的最低版本和你的手机版本
  • 我确实在使用与我用于开发的设备相同的设备。这不是这里的问题。
  • 是签名的apk吗?您是否启用了unknown resources 设置?在某些情况下,如果 apk 文件未正确/完全下载,则可能会发生此错误。
  • 此 APK 已签名,允许使用未知资源。

标签: java android apk android-volley kotlin


【解决方案1】:

问题在于目录。我尝试写入缓存目录,然后从那里安装。将目录更改为context.getExternalFilesDir(&lt;Appname&gt;, "update.apk"),它将毫无问题地安装。

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 2018-08-19
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 2012-06-12
    • 2022-11-30
    • 1970-01-01
    相关资源
    最近更新 更多