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

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

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

指引我,我不知道。 谢谢。

189719 次浏览

试试这个:

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


}

上面的代码对我不起作用。

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

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

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

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

我选择将它作为一个静态实用工具放在一个 helper 类中:

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

这个答案 展示了如果应用程序丢失了,如何从 Play Store 获取应用程序,但是在没有 Play Store 的设备上需要小心。

如果您知道包的名称,那么这种方法不需要使用 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();
}

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

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

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

试试这个

此代码用于检查包名称的应用程序是否已安装或 否则它会打开应用程序的 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 );
}

使用 科特林的更简单的实现

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

可以这样称呼它(搜索 Spotify 应用程序) :

packageManager.isAppInstalled("com.spotify.music")

比已接受的答案(基于 AndroidRate 库)更干净的解决方案(不需要 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;
}

所有的答案只检查某个应用程序是否安装。但是,正如我们所知道的,一个应用程序可以安装,但禁用的用户,不可用。

因此,这个解决方案检查两个。即,安装和启用的应用程序

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 */
}

对其他问题的冷静回答。 例如,如果您不想区分“ 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;
}

通过使用 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
}

和科特林糖一起更好:

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


val packageManager = context.packageManager


return runCatching { packageManager.getPackageInfo(packageName, 0) }.isSuccess
}

你可以使用 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)
}

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

科特林中,最简单的方法可以是两个步骤

1-在 Manifest 中放置目标 应用程序 ID.ex (com.src.Fire)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">


<queries>
<package android:name="com.src.turkey" />
</queries>
...

2-参与活动

 try {


val list = packageManager.getLaunchIntentForPackage("com.src.turkey")
if (list != null) {
Log.i("TAG", "downloadApps:$list")
}


} catch (e: PackageManager.NameNotFoundException) {
Log.i("TAG", "downloadApps: False")
}

没有 任何废弃的这样的

queryIntentActivities


pm.getPackageInfo