我们以编程方式安装了应用程序。
- 如果应用程序已安装在设备中,则应用程序会自动打开。
- 否则安装特定的应用程序。
引导我。我不知道。谢谢。
试试这个:
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;
}
}
比接受的答案更清洁的解决方案(基于这个问题):
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.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;
}
}
使用 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
}
使用Kotlin的更简单的实现
fun PackageManager.isAppInstalled(packageName: String): Boolean =
getInstalledApplications(PackageManager.GET_META_DATA)
.firstOrNull { it.packageName == packageName } != null
并这样称呼它(寻找 Spotify 应用程序):
packageManager.isAppInstalled("com.spotify.music")
比接受的答案(基于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;
}
我认为使用 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;
}
此代码用于检查是否安装了带有包名称的应用程序的天气,如果没有,它将打开您的应用程序的 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 );
}
所有答案仅检查是否安装了某些应用程序。但是,众所周知,一个应用程序可以安装但被用户禁用,无法使用。
因此,此解决方案同时检查两者。即,安装并启用的应用程序。
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 */
}
@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()
}
对其他问题的一个很酷的答案。例如,如果您不想区分“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 suger 更好:
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