重新安装运行Android应用程序自己的APK,然后重新启动应用程序?

NYC*_*ppo 2 android download reinstall application-restart apk

在正在运行的Android应用程序中,我想请求相同的app(1)从我的私人Web服务器重新下载,(2)重新安装自己,然后(3)重新安装后重新启动.

我知道如何执行第1步和第2步,但我还没弄清楚如何执行第3步.

在下载之后,我像这样执行第2步(其中this.apkpath先前已在我的SD卡上设置为下载的APK的完整路径名):

try {
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(Uri.fromFile(new File(this.apkpath)),
                        "application/vnd.android.package-archive");
  this.activity.startActivity(intent);
}
catch (Throwable t) {
  // handle exceptions
}
Run Code Online (Sandbox Code Playgroud)

此代码成功后,我的屏幕上会弹出安装确认对话框,并在此确认后重新安装.但是,安装后,控制权返回到我的桌面管理器,我必须手动重新启动我重新安装的应用程序.

在重新安装后,我可以通过编程方式强制应用程序自动重启?

NYC*_*ppo 6

我想出了重新安装后如何重新启动应用程序.如果它帮助其他人,这就是我做的方式(注意在startActivity之前添加的" addFlags "方法调用):

try {
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(Uri.fromFile(new File(this.apkpath)),
                       "application/vnd.android.package-archive");
  // Add this line ...
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  this.activity.startActivity(intent);
}
catch (Throwable t) {
  // handle exceptions
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我在安装对话框后得到一个打开的请求对话框.然后我可以点击"打开",应用程序确实会重新启动.这足以满足我的需求.