检查应用程序安装 - 机器人机器人、应用程序

2023-09-11 12:46:10 作者:- 你比从前快乐

我试图从谷歌播放安装应用程序。我可以理解,在打开谷歌Play商店的网址,它会打开谷歌的发挥,当我preSS后退按钮,该活动将继续。

I'm trying to install apps from the Google play. I can understand that on opening the google play store url, it opens the google play and when i press the back button, the activity resumes.

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()来检查是否安装了应用程序,但我收到一条错误:

When I went back to the activity, I tried calling this onResume() to check if the app is installed, but i receive an error:

@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 ;
}

的错误是如下:

The error is as follows:

E/AndroidRuntime(796): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appinstaller/com.example.appinstaller.MainActivity}: android.content.ActivityNotFoundException:
No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=com.package.name flg=0x40080000 }

我猜活动的onPause()。有没有更好的方法来实现?我试图检查,如果应用程序已完成安装。

I guess the activity is onPause(). Is there a better way to implement? I'm trying to check if the app has finished installing.

推荐答案

试试这个:

private boolean isPackageInstalled(String packagename, Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (NameNotFoundException e) {
        return false;
    }
}