167

我们以编程方式安装了应用程序。

  1. 如果应用程序已安装在设备中,则应用程序会自动打开。
  2. 否则安装特定的应用程序。

引导我。我不知道。谢谢。

4

16 回答 16

338

试试这个:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Add respective layout
        setContentView(R.layout.main_activity);

        // Use package name which we want to check
        boolean isAppInstalled = appInstalledOrNot("com.check.application");  
        
        if(isAppInstalled) {
            //This intent will help you to launch if the package is already installed
            Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.check.application");
            startActivity(LaunchIntent);
                    
            Log.i("SampleLog", "Application is already installed.");          
        } else {
            // Do whatever we want to do if application not installed
            // For example, Redirect to play store

            Log.i("SampleLog", "Application is not currently installed.");
        }
    }

    private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }

        return false;
    }

}
于 2012-07-09T09:25:20.230 回答
72

比接受的答案更清洁的解决方案(基于这个问题):

public static boolean isAppInstalled(Context context, String packageName) {
    try {
        context.getPackageManager().getApplicationInfo(packageName, 0);
        return true;
    }
    catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

我选择将它作为静态实用程序放在帮助类中。使用示例:

boolean whatsappFound = AndroidUtils.isAppInstalled(context, "com.whatsapp");

这个答案显示了如果应用程序丢失,如何从 Play 商店获取应用程序,但需要注意没有 Play 商店的设备。

于 2015-01-27T16:27:07.503 回答
31

上面的代码对我不起作用。以下方法有效。

使用适当的信息创建一个 Intent 对象,然后使用以下函数检查 Intent 是否可调用:

private boolean isCallable(Intent intent) {  
        List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,   
        PackageManager.MATCH_DEFAULT_ONLY);  
        return list.size() > 0;  
}
于 2013-07-10T03:10:54.630 回答
18

如果您知道包名称,则无需使用 try-catch 块或迭代一堆包即可:

public static boolean isPackageInstalled(Context context, String packageName) {
    final PackageManager packageManager = context.getPackageManager();
    Intent intent = packageManager.getLaunchIntentForPackage(packageName);
    if (intent == null) {
        return false;
    }
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return !list.isEmpty();
}
于 2015-06-08T11:41:56.390 回答
7

此代码检查以确保应用程序已安装,但也检查以确保它已启用。

private boolean isAppInstalled(String packageName) {
    PackageManager pm = getPackageManager();
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return pm.getApplicationInfo(packageName, 0).enabled;
    }
    catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
于 2016-05-20T21:40:46.690 回答
5

使用 kotlin 检查 Android 中是否安装了应用程序。

创建 kotlin 扩展。

fun PackageManager.isAppInstalled(packageName: String): Boolean = try {
        getApplicationInfo(packageName, PackageManager.GET_META_DATA)
        true
    } catch (e: Exception) {
        false
    }

现在,可以检查应用程序是否已安装

if (packageManager.isAppInstalled("AppPackageName")) {
    // App is installed
}else{
    // App is not installed
}
于 2020-01-16T05:59:16.117 回答
4

使用Kotlin的更简单的实现

fun PackageManager.isAppInstalled(packageName: String): Boolean =
        getInstalledApplications(PackageManager.GET_META_DATA)
                .firstOrNull { it.packageName == packageName } != null

并这样称呼它(寻找 Spotify 应用程序):

packageManager.isAppInstalled("com.spotify.music")
于 2017-12-27T20:02:20.110 回答
4

比接受的答案(基于AndroidRate Library )更清洁的解决方案(没有 try-catch ):

public static boolean isPackageExists(@NonNull final Context context, @NonNull final String targetPackage) {
    List<ApplicationInfo> packages = context.getPackageManager().getInstalledApplications(0);
    for (ApplicationInfo packageInfo : packages) {
        if (targetPackage.equals(packageInfo.packageName)) {
            return true;
        }
    }
    return false;
}
于 2018-04-09T14:10:41.337 回答
3

我认为使用 try/catch 模式对性能来说不是很好。我建议使用这个:

public static boolean appInstalledOrNot(Context context, String uri) {
    PackageManager pm = context.getPackageManager();
    List<PackageInfo> packageInfoList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);
    if (packageInfoList != null) {
        for (PackageInfo packageInfo : packageInfoList) {
            String packageName = packageInfo.packageName;
            if (packageName != null && packageName.equals(uri)) {
                return true;
            }
        }
    }
    return false;
}
于 2016-10-09T13:52:26.663 回答
1

试试这个

此代码用于检查是否安装了带有包名称的应用程序的天气,如果没有,它将打开您的应用程序的 Playstore 链接,否则您已安装的应用程序

String your_apppackagename="com.app.testing";
    PackageManager packageManager = getPackageManager();
    ApplicationInfo applicationInfo = null;
    try {
        applicationInfo = packageManager.getApplicationInfo(your_apppackagename, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    if (applicationInfo == null) {
        // not installed it will open your app directly on playstore
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + your_apppackagename)));
    } else {
        // Installed
        Intent LaunchIntent = packageManager.getLaunchIntentForPackage(your_apppackagename);
        startActivity( LaunchIntent );
    }
于 2017-09-04T07:24:37.783 回答
1

所有答案仅检查是否安装了某些应用程序。但是,众所周知,一个应用程序可以安装但被用户禁用,无法使用。

因此,此解决方案同时检查两者。即,安装并启用的应用程序

public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
     try {
          return packageManager.getApplicationInfo(packageName, 0).enabled;
     }
     catch (PackageManager.NameNotFoundException e) {
          return false;
     }
}

调用方法isPackageInstalled()

boolean isAppInstalled = isPackageInstalled("com.android.app" , this.getPackageManager());

现在,使用布尔变量isAppInstalled并做任何你想做的事情。

if(isAppInstalled ) {
    /* do whatever you want */
}
于 2018-08-26T17:50:12.327 回答
0

@Egemen Hamutçu 在 kotlin B-) 中的回答

    private fun isAppInstalled(context: Context, uri: String): Boolean {
        val packageInfoList = context.packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES)
        return packageInfoList.asSequence().filter { it?.packageName == uri }.any()
    }
于 2017-09-01T12:44:05.650 回答
0

对其他问题的一个很酷的答案。例如,如果您不想区分“com.myapp.debug”和“com.myapp.release”!

public static boolean isAppInstalled(final Context context, final String packageName) {
    final List<ApplicationInfo> appsInfo = context.getPackageManager().getInstalledApplications(0);
    for (final ApplicationInfo appInfo : appsInfo) {
        if (appInfo.packageName.contains(packageName)) return true;
    }
    return false;
}
于 2018-12-03T10:46:17.253 回答
0

使用 Kotlin suger 更好:

  private fun isSomePackageInstalled(context: Context, packageName: String): Boolean {

    val packageManager = context.packageManager

    return runCatching { packageManager.getPackageInfo(packageName, 0) }.isSuccess
  }
于 2020-02-18T15:19:23.457 回答
0

您可以使用 Kotlin 扩展来做到这一点:

fun Context.getInstalledPackages(): List<String> {
    val packagesList = mutableListOf<String>()
    packageManager.getInstalledPackages(0).forEach {
        if ( it.applicationInfo.sourceDir.startsWith("/data/app/") && it.versionName != null)
            packagesList.add(it.packageName)
    }
    return packagesList
}

fun Context.isInDevice(packageName: String): Boolean {
    return getInstalledPackages().contains(packageName)
}
于 2020-09-15T07:36:45.423 回答
0

Android 11 更新
您必须在清单中指定要搜索的确切捆绑包 ID。

facebook 和 whatsapp 的示例:

在“应用程序”上方的清单内(权限所在)

<queries>
    <package android:name="com.whatsapp" />
    <package android:name="com.facebook.katana" />
</queries>  

这将允许您检查是否安装了 facebook 和 whatsapp,否则您将始终得到错误的检查。

关于该主题的进一步阅读:
https ://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9

于 2022-01-26T21:47:35.857 回答