如何以编程方式检查应用程序是否已安装在 Android 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11392183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check programmatically if an application is installed or not in Android?
提问by Sathish Sathish
We have installed applications programmatically.
我们已经以编程方式安装了应用程序。
- If the application is already installed in the device the application is open automatically.
- Otherwise install the particular application.
- 如果该应用程序已安装在设备中,该应用程序将自动打开。
- 否则安装特定的应用程序。
Guide Me. I have no idea. Thanks.
引导我。我不知道。谢谢。
回答by Aerrow
Try with this:
试试这个:
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;
}
}
回答by Jonik
Somewhat cleaner solution than the accepted answer (based on this question):
比接受的答案更简洁的解决方案(基于这个问题):
public static boolean isAppInstalled(Context context, String packageName) {
try {
context.getPackageManager().getApplicationInfo(packageName, 0);
return true;
}
catch (PackageManager.NameNotFoundException e) {
return false;
}
}
I chose to put it in a helper class as a static utility. Usage example:
我选择将它作为静态实用程序放在帮助程序类中。用法示例:
boolean whatsappFound = AndroidUtils.isAppInstalled(context, "com.whatsapp");
This answershows how to get the app from the Play Store if the app is missing, though care needs to be taken on devices that don't have the Play Store.
这个答案显示了如果应用程序丢失,如何从 Play 商店获取应用程序,但需要注意没有 Play 商店的设备。
回答by Priyank Desai
The above code didn't work for me. The following approach worked.
上面的代码对我不起作用。以下方法有效。
Create an Intent object with appropriate info and then check if the Intent is callable or not using the following function:
创建一个具有适当信息的 Intent 对象,然后使用以下函数检查 Intent 是否可调用:
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
回答by Kavi
If you know the package name, then this works without using a try-catch block or iterating through a bunch of packages:
如果您知道包名称,则无需使用 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();
}
回答by youravgjoe
This code checks to make sure the app is installed, but also checks to make sure it's enabled.
此代码检查以确保应用程序已安装,但也会检查以确保它已启用。
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;
}
}
回答by Egemen Hamut?u
I think using try/catch pattern is not very well for performance. I advice to use this:
我认为使用 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;
}
回答by Alexander Savin
Cleaner solution (without try-catch) than the accepted answer (based on AndroidRate Library):
比接受的答案(基于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;
}
回答by Neeraj Dhameliya
Check App is installed or not in Android by using kotlin.
使用 kotlin 检查 Android 中是否安装了应用程序。
Creating kotlin extension.
创建 kotlin 扩展。
fun PackageManager.isAppInstalled(packageName: String): Boolean = try {
getApplicationInfo(packageName, PackageManager.GET_META_DATA)
true
} catch (e: Exception) {
false
}
Now, can check if app is install or not
现在,可以检查应用程序是否安装
if (packageManager.isAppInstalled("AppPackageName")) {
// App is installed
}else{
// App is not installed
}
回答by Sunil
Try this
尝试这个
This code is used to check weather your application with package name is installed or not if not then it will open playstore link of your app otherwise your installed app
此代码用于检查是否安装了带有包名称的应用程序的天气,如果没有,它将打开您的应用程序的 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 );
}
回答by Víctor Albertos
A simpler implementation using Kotlin
使用Kotlin的更简单实现
fun PackageManager.isAppInstalled(packageName: String): Boolean =
getInstalledApplications(PackageManager.GET_META_DATA)
.firstOrNull { it.packageName == packageName } != null
And call it like this (seeking for Spotify app):
并这样称呼它(寻求 Spotify 应用程序):
packageManager.isAppInstalled("com.spotify.music")