检查是否安装了应用程序 - Android

检查是否安装了应用程序 - Android

我正在尝试从Google Play安装应用。我可以理解,在打开Google Play商店网址时,它会打开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()来检查应用程序是否已安装,但是我收到错误:

@Overrideprotected 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()。有没有更好的方法来实现它?我正在尝试检查应用是否已完成安装。


蝴蝶刀刀
浏览 451回答 3
3回答

哈士奇WWW

试试这个:public&nbsp;static&nbsp;boolean&nbsp;isAvailable(Context&nbsp;ctx,&nbsp;Intent&nbsp;intent)&nbsp;{final&nbsp;PackageManager&nbsp;mgr&nbsp;=&nbsp;ctx.getPackageManager();List<ResolveInfo>&nbsp;list&nbsp;=&nbsp;&nbsp;&nbsp;mgr.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);return&nbsp;list.size()&nbsp;>&nbsp;0;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android