0

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

  1. Start app
  2. Check if there is a new version of the app on the net
  3. If there is, just download the apk
  4. 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))
Apuna12
  • 375
  • 2
  • 6
  • 23
  • did you try fixing the error? – Tim Oct 08 '19 at 10:47
  • You need a fileprovider to open a file like this. And for your information you need INSTALL_PACKAGES permission. follow this https://stackoverflow.com/questions/5803999/install-apps-silently-with-granted-install-packages-permission – Sanket Bhat Oct 08 '19 at 10:48
  • yes, but I am not quite sure how to fix it – Apuna12 Oct 08 '19 at 10:48
  • @SanketBhat I have permission... :) – Apuna12 Oct 08 '19 at 10:49
  • @Apuna12 then you need file provider https://developer.android.com/reference/android/support/v4/content/FileProvider – Sanket Bhat Oct 08 '19 at 10:51
  • @SanketBhat I tried this right now... looks like it's doing the right thing... but now I am getting an error in the phone "There was a problem while parsing the package"... When i click OK and then find the file manually in the smartphone and I try to install it.. it's working... do you have any suggestions?... Also I'll add my findings into the question – Apuna12 Oct 08 '19 at 12:19
  • @Apuna12 Did you add proper READ permission to that Uri before? like `intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)` – Sanket Bhat Oct 08 '19 at 12:57
  • @SanketBhat Yes I did it 5 minutes ago maybe?... Now I have another type of error... Now I am able to install the app, but it's showing "App not installed." – Apuna12 Oct 08 '19 at 13:02
  • 1
    @Apuna12 That is because you are trying to install apps with different signatures. For example you can not install release build when debug build is installed. They have different app signature. If both are of same type and same signature it works without any problem – Sanket Bhat Oct 08 '19 at 13:09
  • @SanketBhat worked like a charm :)... now it's working :)) Thanks very much for assistance and help :) – Apuna12 Oct 08 '19 at 13:31

0 Answers0