检查应用程序是否已安装 - Android [英] Check if application is installed - Android

查看:43
本文介绍了检查应用程序是否已安装 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Google Play 安装应用.我可以理解,在打开 Google Play 商店 URL 时,它会打开 Google Play,当我按下后退按钮时,活动会恢复.

I'm trying to install apps from 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);

当我回到 Activity 时,我尝试调用这个 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 ;
}

错误如下:

E/AndroidRuntime(796): java.lang.RuntimeException: 无法启动活动组件信息{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 }

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 it? I'm trying to check if the app has finished installing.

推荐答案

试试这个:

private boolean isPackageInstalled(String packageName, PackageManager packageManager) {
    try {
        packageManager.getPackageInfo(packageName, 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

它尝试获取有关您传入的名称的包的信息.如果失败,如果抛出 NameNotFoundException,则意味着没有安装具有该名称的包,因此我们返回 错误.

It attempts to fetch information about the package whose name you passed in. Failing that, if a NameNotFoundException was thrown, it means that no package with that name is installed, so we return false.

请注意,我们传入的是 PackageManager 而不是 Context,因此该方法的使用更加灵活,并且不会违反 德米特法则.您可以在不访问 Context 实例的情况下使用该方法,只要您有一个 PackageManager 实例.

Note that we pass in a PackageManager instead of a Context, so that the method is slightly more flexibly usable and doesn't violate the law of Demeter. You can use the method without access to a Context instance, as long as you have a PackageManager instance.

像这样使用它:

public void someMethod() {
    // ...
    
    PackageManager pm = context.getPackageManager();
    boolean isInstalled = isPackageInstalled("com.somepackage.name", pm);
    
    // ...
}

注意:从 Android 11 (API 30) 开始,您可能需要在清单中声明 ,具体取决于您要查找的包.查看文档了解更多信息.

Note: From Android 11 (API 30), you might need to declare <queries> in your manifest, depending on what package you're looking for. Check out the docs for more info.

这篇关于检查应用程序是否已安装 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆