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.
指导我。我也不知道。谢谢。
10 个解决方案
#1
275
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;
}
}
#2
49
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:
我选择将它作为静态实用程序放在helper类中。使用的例子:
boolean whatsappFound = AndroidUtils.isAppInstalled(context, "com.whatsapp");
This answer shows 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 Store中获取应用程序,不过需要注意没有Play Store的设备。
#3
22
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:
创建一个带有适当信息的意图对象,然后检查该意图是否可调用,或不使用以下功能:
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
#4
12
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();
}
#5
5
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;
}
}
#6
2
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;
}
#7
2
Cleaner solution (without try-catch) than the accepted answer (based on AndroidRate Library):
比公认答案(基于android库)更干净的解决方案(没有试戴):
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;
}
#8
1
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 );
}
#9
0
@Egemen Hamutçu s answer in kotlin B-)
@Egemen Hamutcu的答案在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()
}
#10
0
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")
#1
275
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;
}
}
#2
49
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:
我选择将它作为静态实用程序放在helper类中。使用的例子:
boolean whatsappFound = AndroidUtils.isAppInstalled(context, "com.whatsapp");
This answer shows 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 Store中获取应用程序,不过需要注意没有Play Store的设备。
#3
22
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:
创建一个带有适当信息的意图对象,然后检查该意图是否可调用,或不使用以下功能:
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
#4
12
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();
}
#5
5
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;
}
}
#6
2
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;
}
#7
2
Cleaner solution (without try-catch) than the accepted answer (based on AndroidRate Library):
比公认答案(基于android库)更干净的解决方案(没有试戴):
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;
}
#8
1
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 );
}
#9
0
@Egemen Hamutçu s answer in kotlin B-)
@Egemen Hamutcu的答案在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()
}
#10
0
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")