如何检查是否安装了应用程序

时间:2013-09-12 17:45:41

标签: android android-intent

如何使用getPackageManager检查应用是否已安装? 我不知道。谢谢。 机器人。

    Intent intent = new Intent();
    intent.setClassName("PACKAGE_NAME", "PACKAGE_NAME.TARGET_ACTIVITY");
    if (isCallable(context, intent)) {
        // Attach any extras, start or start with callback
    } else {
        // Respond to the application or activity not being available
    }

4 个答案:

答案 0 :(得分:0)

protected boolean isAppInstalled(String packageName) {
        Intent mIntent = getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);
        if (mIntent != null) {
            return true;
        }
        else {
            return false;
        }
    }

答案 1 :(得分:0)

您可以使用以下代码段

获取已安装应用的列表
PackageManager packageManager = getPackageManager();
  List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)

现在,您可以获取每个已安装应用的包名称,然后可以检查包名称

ApplicationInfo data = appsList.get(position);
String packageName = data.packageName;

查看另一个示例How to Get List of Installed Apps in Android

答案 2 :(得分:0)

我认为queryIntentActivities更适合,因为您的意图中定义了目标活动。如果列表返回为.size() == 0。然后可以肯定地说它不可用。

protected boolean isCallable(Context context, Intent intent) {
    List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent, 0);
    return activities.size() > 0 ? true : false;
}

答案 3 :(得分:0)

试试这个:

private boolean appInstalledorNot(String packageName) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
    pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
    app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
    app_installed = false;
}
return app_installed;
}