如何以编程方式检查是否安装了应用程序?

我们已经编程安装了应用程序。

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

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

试试这个:

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("Application is already installed."); } else { // Do whatever we want to do if application not installed // For example, Redirect to play store Log.i("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; } } 

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

 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商店的设备上进行操作。

上面的代码没有为我工作。 以下方法奏效。

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

 private boolean isCallable(Intent intent) { List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; } 

如果你知道包的名字,那么这个工作不需要使用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.size() > 0; } 

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

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

我认为使用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; } 

@EmenmenHamutç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() } 

尝试这个

这个代码是用来检查天气你的应用程序与包的名称是否安装,否则将打开您的应用程序的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 ); }