Я пытаюсь реализовать функцию обновления с помощью apk-файла с сервера.
private fun openDownloadLinkAndKill(url: String) {
Log.d("test", "url: $url")
// startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
// val promptInstall = Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse(url), "application/vnd.android.package-archive")
// startActivity(promptInstall)
//get destination to update file and set Uri
//TODO: First I wanted to store my update .apk file on internal storage for my app but apparently android does not allow you to open and install
//aplication with existing package from there. So for me, alternative solution is Download directory in external storage. If there is better
//solution, please inform us in comment
var destination = "${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)}/"
Log.d("test", "$destination")
val fileName = "my_android.apk"
destination = "$destination$fileName"
val uri = Uri.parse("content://$destination");
//Delete update file if exists
val file = File(destination);
if (file.exists())
//file.delete() - test this, I think sometimes it doesn't work
file.delete()
//set downloadmanager
val request = DownloadManager.Request(Uri.parse(url))
request.setDescription("App is updating.")
request.setTitle(APPLICATION_NAME)
//set destination
request.setDestinationUri(uri)
// get download service and enqueue file
val manager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val downloadId = manager.enqueue(request)
//set BroadcastReceiver to install app when .apk is downloaded
val onComplete: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val install = Intent(Intent.ACTION_VIEW)
install.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
install.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId))
startActivity(install)
unregisterReceiver(this)
finish()
}
}
//register receiver for when .apk download is compete
registerReceiver(onComplete, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out)
MyApp.getInstance().killAllActivities(this)
}
И я получаю эту ошибку:
java.lang.IllegalArgumentException: Not a file URI: content:///storage/emulated/0/Download/my_android.apk
Я думаю, путь указан неверно. Кроме того, я хотел бы показать прогресс обновления с индикатором выполнения, который показывает его процент от 0% до 100%.
Как я могу реализовать это? И какое разрешение мне нужно?