我正在尝试从 Google Play 安装应用程序。我可以理解,在打开 Google Play 商店 URL 时,它会打开 Google Play,当我按下后退按钮时,活动会继续。
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(appURL));
marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(marketIntent);
当我回到活动时,我尝试调用它onResume()
来检查应用程序是否已安装,但我收到一个错误:
@Override
protected void onResume() {
super.onResume();
boolean installed = false;
while (!installed) {
installed = appInstalledOrNot(APPPACKAGE);
if (installed) {
Toast.makeText(this, "App installed", Toast.LENGTH_SHORT).show();
}
}
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed ;
}
错误如下:
E/AndroidRuntime(796): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.appinstaller/com.example.appinstaller.MainActivity}: android.content.ActivityNotFoundException: 找不到活动来处理 Intent { act=android .intent.action.VIEW dat=market://details?id=com.package.name flg=0x40080000 }
我猜活动是onPause()
。有没有更好的方法来实现它?我正在尝试检查应用程序是否已完成安装。