如何解决在自动更新过程中安装旧版本
我觉得通过 CDN 更新真的很不安全。现在我在安装新版本时遇到问题。
我在同一个存储桶和文件夹中上传了 1.0.14、1.0.15、1.0.16 版本。
我测试了这些更新。但是,当我测试 1.0.16 时,我当前的版本是 1.0.15(由 adb 安装)并显示更新弹出窗口,但是当我更新时它安装了 1.0.14。
我尝试删除应用程序的缓存和 S3 文件夹的版本。但是没有用。
当我通过文件资源管理器检查设备时,它似乎创建了较旧的 apk .. URL 和它的文件名很好。但我不知道。这是怎么回事。
private class DownloadTask(context: Context) : AsyncTask<String,Int,String?>() {
var context: Context? = context
var mWakeLock: PowerManager.WakeLock? = null
//set destination
val fileName = NetworkConstants.APK_NAME
var destination = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
"${context.getExternalFilesDir(null)}/$fileName"
} else {
"${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)}/$fileName"
}
override fun doInBackground(vararg params: String?): String? {
var input: InputStream? = null
var output: OutputStream? = null
var connection: HttpURLConnection? = null
try {
val url = URL(params[0])
connection = url.openConnection() as HttpURLConnection
connection.connect()
// expect HTTP 200 OK,so we don't mistakenly save error report
// instead of the file
if (connection.responseCode != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP ${connection.responseCode}\n${connection.responseMessage}"
}
// this will be useful to display download percentage
// might be -1: server did not report the length
val fileLength = connection.contentLength
// download the file
input = connection.inputStream
output = FileOutputStream(destination)
val data = ByteArray(4096)
var total = 0
var count = 0
while ({ count = input.read(data); count }() != -1) {
// allow canceling with back button
if (isCancelled) {
input.close()
return null
}
total += count
// publishing the progress...
if (fileLength > 0) {
// only if total length is kNown
// Log.d("download","${total}/${count}/${fileLength}")
publishProgress(total,fileLength)
}
output.write(data,count)
}
} catch (e: Exception) {
return e.toString()
} finally {
try {
output?.close()
input?.close()
} catch (e: IOException) {
e.printstacktrace()
}
connection?.disconnect()
}
return null
}
override fun onProgressUpdate(vararg values: Int?) {
super.onProgressUpdate(*values)
// if we get here,length is kNown,Now set indeterminate to false
mProgressDialog!!.isIndeterminate = false
mProgressDialog!!.max = values[1] as Int
mProgressDialog!!.progress = values[0] as Int
}
override fun onPreExecute() {
super.onPreExecute()
// take cpu lock to prevent cpu from going off if the user
// presses the power button during download
val pm = context!!.getSystemService(Context.POWER_SERVICE) as PowerManager
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,javaClass.name)
mWakeLock!!.acquire() // no need to set time
mProgressDialog!!.show()
deleteApk()
}
override fun onPostExecute(result: String?) {
mWakeLock?.release()
mProgressDialog?.dismiss()
if (result != null) {
Toast.makeText(context,"Update Failed: $result",Toast.LENGTH_LONG).show()
MyApplication.getInstance().restartOne()
} else {
Toast.makeText(context,"Updating...",Toast.LENGTH_SHORT).show()
val file = File(destination)
installApk(file)
}
}
fun installApk(file: File) {
val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.d("test",BuildConfig.APPLICATION_ID)
FileProvider.getUriForFile(
context!!,"${BuildConfig.APPLICATION_ID}.fileprovider",file
)
} else {
Uri.fromFile(file)
}
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri,"application/vnd.android.package-archive")
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
context!!.startActivity(intent)
(context as EntryActivity).overridePendingTransition(
R.anim.abc_fade_in,R.anim.abc_fade_out
)
(context as EntryActivity).finish()
}
/**
* Delete PDF_FINDER apk for
*/
private fun deleteApk() {
val fileName = NetworkConstants.APK_NAME
val destination = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
"${context!!.getExternalFilesDir(null)}/$fileName"
} else {
"${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)}/$fileName"
}
val file = File(destination)
if (file.exists()) {
file.delete()
}
}
我想使用相同的名称,这样它就不会多次下载并且一个应用程序有一百个apk。我不确定我是否正确删除。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。