I decided to make my own updating system and I encountered a problem which I am not sure how to fix.
I already tried several things but every one of them ended the same way
private fun startDownloading() {
var networkTask = NetworkTask(this)
networkTask.execute()
val url = "http://downloadsite.com/myApk.apk"
var downloadmanager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
var uri = Uri.parse(url)
var request = DownloadManager.Request(uri)
var filename = "app-release.apk"
var fullPath: String =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() +
"/" + filename
val myFile = File(fullPath)
if (myFile.exists())
myFile.delete()
request.setTitle("My File")
request.setDescription("Downloading")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(false)
request.setDestinationUri(
Uri.parse("file://" + fullPath)
)
downloadmanager.enqueue(request)
var onComplete:BroadcastReceiver = object:BroadcastReceiver() {
override fun onReceive(context:Context, intent:Intent) {
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(Uri.fromFile(myFile), "application/vnd.android.package-archive")
startActivity(intent)
}
}
registerReceiver(onComplete, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
}
and I am always getting this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: sk.letsdream, PID: 30431
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=sk.letsdream (has extras) } in sk.letsdream.LoginActivity$startDownloading$onComplete$1@e2488a5
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1641)
at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8108)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Download/app-release.apk exposed beyond app through Intent.getData()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:2083)
at android.net.Uri.checkFileUriExposed(Uri.java:2393)
at android.content.Intent.prepareToLeaveProcess(Intent.java:11014)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10967)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1725)
at android.app.Activity.startActivityForResult(Activity.java:5326)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:5267)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at android.app.Activity.startActivity(Activity.java:5697)
at android.app.Activity.startActivity(Activity.java:5665)
at sk.letsdream.LoginActivity$startDownloading$onComplete$1.onReceive(LoginActivity.kt:658)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1631)
at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8108)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
I want to something like this
- Start app
- Check if there is a new version of the app on the net
- If there is, just download the apk
- After the download is finished install the new version of the app
Do I have an error in the code ?
EDIT
I tried to do this like this. It seems to be doing it's thing, but I am getting an error "There was a problem while parsing the package" in the smartphone.... Here is an additional code I used
var fullPath: String =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() +
"/" + filename
val myFile = File(fullPath)
var onComplete:BroadcastReceiver = object:BroadcastReceiver() {
override fun onReceive(context:Context, intent:Intent) {
val uri = FileProvider.getUriForFile(
this@LoginActivity,
BuildConfig.APPLICATION_ID + ".provider",
myFile
)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "application/vnd.android.package-archive")
startActivity(intent)
exitProcess(-1)
}
}
registerReceiver(onComplete, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))