最佳答案
我正在尝试从 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: 无法启动 活动 组件信息{ com.example.appinstaller/com.example.appinstaller. MainActivity } : 异常: 没有找到处理意图{ act = android.Intent.action.VIEW 的活动 Dat = market://Details? id = com.package.name flg = 0x40080000}
我猜测活动是 onPause()
。有没有更好的方法来实现它?我正在检查应用程序是否已经安装完毕。