Error while trying to auto-update the program

Viewed 50

I have implemented auto-update in the program, but I cannot understand why it does not work. He finds a file with the .apk extension. If you just run the program and try to update it, it does not crash. But when you go through with a debugger, it crashes. Although everything is going as it should. Please see if I missed something. I am grateful for the early

 File fDir = new File(GlobalVars.AppPathData());
                File[] files = fDir.listFiles(FilenameFilterAPK);
                if (files.length > 0) {
                    Toast.makeText(this.getApplicationContext(), "Обновление программы...", Toast.LENGTH_SHORT).show();
                    File file = lastFile(files);
                    Uri fileUri = Uri.fromFile(file);

                    try {
                        if (Build.VERSION.SDK_INT >= 24) {
                            Log.d(GlobalVars.LOG_TAG, this.getPackageName());
                            fileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), file);
                            Toast.makeText(this.getApplicationContext(), fileUri.toString(), Toast.LENGTH_LONG).show();
                        }

                        Intent intent1 = new Intent(Intent.ACTION_INSTALL_PACKAGE);
                        intent1.setData(fileUri);
                        intent1.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        startActivity(intent1);
                    }
                    catch (Exception e){
                        Log.d(GlobalVars.LOG_TAG, e.getMessage());
                        Toast.makeText(this, "Ошибка обновления программы", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "Обновления программы нет", Toast.LENGTH_SHORT).show();
                }

ERROR

 Process: com.example.personasdelibery, PID: 19371
    android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@89688da is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:690)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:353)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
        at android.widget.Toast$TN.handleShow(Toast.java:490)
        at android.widget.Toast$TN$2.handleMessage(Toast.java:358)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6377)
0 Answers
Related