How to detect if Google Play is installed? (Not Market) - android

Whether the app installed is called Google Play or Market, the package name is the same com.android.vending.
I need to be able to detect whether the app is Google Play or Market, I've checked in PackageInfo and nothing except versionCode and versionName can be of help.
Does anyone know what the first versionCode was or versionName was for Google Play app?
If anyone knows any other way of detecting this let me know.

I figured out how to check the application label. I was using the debugger to see what all was being returned in packageInfo that's why I didn't see it initially.
public static boolean isGooglePlayInstalled(Context context) {
PackageManager pm = context.getPackageManager();
boolean app_installed = false;
try
{
PackageInfo info = pm.getPackageInfo("com.android.vending", PackageManager.GET_ACTIVITIES);
String label = (String) info.applicationInfo.loadLabel(pm);
app_installed = (label != null && !label.equals("Market"));
}
catch (PackageManager.NameNotFoundException e)
{
app_installed = false;
}
return app_installed;
}

You can also try this much simplified solution:
public boolean isGooglePlayAvailable() {
boolean googlePlayStoreInstalled;
int val= GooglePlayServicesUtil.isGooglePlayServicesAvailable(LocationActivity.this);
googlePlayStoreInstalled = val == ConnectionResult.SUCCESS;
return googlePlayStoreInstalled;
}

In my App I check possibility to open play store before fire it like:
public static boolean isResolveActivity(Intent intent) {
return App.getInstance().getPackageManager().resolveActivity(intent, PackageManager.GET_RESOLVED_FILTER) != null;
}
public void isResolveActivity(String appPackage) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackage));
if(isResolveActivity(intent)){
...open intent
}
}

You can use this simple piece of code, its easy and to the point with a consideration for not re-inventing the wheel using GooglePlayServicesUtil:
public static boolean isPlayStoreInstalled(Context context){
try {
context.getPackageManager()
.getPackageInfo(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
This will require you to add this to your dependencies:
compile 'com.google.android.gms:play-services-base:[PLAY_SERVICES_VERSION]'
Latest play-services version is now: 10.0.1

This is probably a better example as it allows for status' where the user can do something about it i.e re-auth or update. Based on the code in the GCM client example project:
public static boolean checkPlayServices(Activity activity) {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, activity,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(activity.getApplicationContext(), "This device is not supported.", Toast.LENGTH_LONG).show();
activity.finish();
}
return false;
}
return true;
}

Related

My application gets force closed when I try to launch another application [duplicate]

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.
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("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;
}
}
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 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.
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;
}
If you know the package name, then this works without using a try-catch block or iterating through a bunch of packages:
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();
}
Android 11 update
You have to specify in the manifest the exact bundle id's you want to search for.
Example for facebook and whatsapp:
Inside the Manifest above "application" (where the permissions are)
<queries>
<package android:name="com.whatsapp" />
<package android:name="com.facebook.katana" />
</queries>
This will allow you to check if facebook and whatsapp are installed, otherwise you will always get false for that check.
Further reading on the subject:
https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9
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;
}
}
Check App is installed or not in Android by using kotlin.
Creating kotlin extension.
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
}
A simpler implementation using 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):
packageManager.isAppInstalled("com.spotify.music")
Cleaner solution (without try-catch) than the accepted answer (based on AndroidRate Library):
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;
}
I think using try/catch pattern is not very well for performance. I advice to use this:
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;
}
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
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 );
}
All the answers only check certain app is installed or not. But, as we all know an app can be installed but disabled by the user, unusable.
Therefore, this solution checks for both. i.e, installed AND enabled apps.
public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
try {
return packageManager.getApplicationInfo(packageName, 0).enabled;
}
catch (PackageManager.NameNotFoundException e) {
return false;
}
}
Call the method isPackageInstalled():
boolean isAppInstalled = isPackageInstalled("com.android.app" , this.getPackageManager());
Now, use the boolean variable isAppInstalled and do whatever you want.
if(isAppInstalled ) {
/* do whatever you want */
}
#Egemen Hamutçu s answer in 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()
}
A cool answer to other problems.
If you do not want to differentiate "com.myapp.debug" and "com.myapp.release" for example !
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;
}
So nicer with Kotlin suger:
private fun isSomePackageInstalled(context: Context, packageName: String): Boolean {
val packageManager = context.packageManager
return runCatching { packageManager.getPackageInfo(packageName, 0) }.isSuccess
}
You can do it using Kotlin extensions :
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)
}
In Kotlin, the simplest way can be two steps
1- in the Manifest put the target app id . ex (com.src.turkey)
<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- In an Activity
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")
}
There isn't any deprecated such as
queryIntentActivities
pm.getPackageInfo

Check the availability of GoogleMaps on Android if not direct to Play store for install

I implement a drawing route app on GoogleMaps and that is working but first I need to check for GoogleMaps availablity on Android or not. If the app does not exist, the user should be directed to Google Play Store to install GoogleMaps.
How can I prompt the user to install it?
Try this:
private boolean isPackageInstalled(String packagename, PackageManager packageManager) {
try {
packageManager.getPackageInfo(packagename, 0);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
It attempts to fetch information about the package whose name you passed in. Failing that, if a NameNotFoundException was thrown, it means that no package with that name is installed, so we return false.
in your case
public void someMethod() {
// ...
String googleMapsPackageName = "com.google.android.apps.maps"
PackageManager pm = context.getPackageManager();
boolean isInstalled = isPackageInstalled(googleMapsPackageName, pm);
if(isInstalled)
// go to maps
else
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + googleMapsPackageName)));
// ...
}

Integrating Ola money api in android always getting app not installed?

Hello all i am integrating ola money with in my android app now the problem i am facing is that in their docs it is given that first check for whether the app is installed and for that they have given below code to see that, now i am having ola cabs app installed in my android device but this function is returning false i dont know what i am doing wrong, if somebody has integrated ola in android please tellme how is it working for you
private boolean check_olacabs() {
try {
context.getPackageManager().getApplicationInfo("com.test.olacabs", 0);
return true;
} catch (Exception e) {
return false;
}
}
Thank you in advance
Replace your code with this.
private boolean check_olacabs() {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo("com.olacabs.customer", PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

Using If and else to change to change layout file # runtime

I am currently working on a project that i am trying to add an icon making competition and the winner will receive a special app plugin that only the contest winner will get and i would like my app to check if the package(special app plugin) exists on the users device and if it does i would like the app to display an alternate display on run-time.Would i use "if"
and "else" statements to achieve this and if so how would i go about this and Thanks in advanced.
Note:I have successfully made the app load a different layout depending on the android version so i have a little bit of an idea but need some help.
Check this to know app is installed or not..
public class Example extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Put the package name here...
boolean installed = appInstalledOrNot("com.Ch.Example.pack");
if(installed)
{
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.Ch.Example.pack");
startActivity(LaunchIntent);
System.out.println("App already installed om your phone");
}
else
{
System.out.println("App is not installed om your phone");
}
}
private boolean appInstalledOrNot(String uri)
{
PackageManager pm = getPackageManager();
boolean app_installed = false;
try
{
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e)
{
app_installed = false;
}
return app_installed ;
}
}
Well if your plugin has a unique name(i am sure it should) you can check it.
Please read the post linked below for checking whether the plugin exists or not
Link : https://stackoverflow.com/a/6758962/1542720
Hope this helps !
you can change your xml file according to your requirement in onCreate()..
as i did in my code, for the different density i used different xml file.
if (metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM) {
setContentView(R.layout.activity_main);
} else if (metrics.densityDpi == DisplayMetrics.DENSITY_LOW) {
setContentView(R.layout.activity_main_small);
} else {
setContentView(R.layout.activity_main_large);
}

How to check programmatically if an application is installed or not in Android?

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.
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("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;
}
}
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 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.
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;
}
If you know the package name, then this works without using a try-catch block or iterating through a bunch of packages:
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();
}
Android 11 update
You have to specify in the manifest the exact bundle id's you want to search for.
Example for facebook and whatsapp:
Inside the Manifest above "application" (where the permissions are)
<queries>
<package android:name="com.whatsapp" />
<package android:name="com.facebook.katana" />
</queries>
This will allow you to check if facebook and whatsapp are installed, otherwise you will always get false for that check.
Further reading on the subject:
https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9
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;
}
}
Check App is installed or not in Android by using kotlin.
Creating kotlin extension.
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
}
A simpler implementation using 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):
packageManager.isAppInstalled("com.spotify.music")
Cleaner solution (without try-catch) than the accepted answer (based on AndroidRate Library):
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;
}
I think using try/catch pattern is not very well for performance. I advice to use this:
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;
}
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
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 );
}
All the answers only check certain app is installed or not. But, as we all know an app can be installed but disabled by the user, unusable.
Therefore, this solution checks for both. i.e, installed AND enabled apps.
public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
try {
return packageManager.getApplicationInfo(packageName, 0).enabled;
}
catch (PackageManager.NameNotFoundException e) {
return false;
}
}
Call the method isPackageInstalled():
boolean isAppInstalled = isPackageInstalled("com.android.app" , this.getPackageManager());
Now, use the boolean variable isAppInstalled and do whatever you want.
if(isAppInstalled ) {
/* do whatever you want */
}
#Egemen Hamutçu s answer in 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()
}
A cool answer to other problems.
If you do not want to differentiate "com.myapp.debug" and "com.myapp.release" for example !
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;
}
So nicer with Kotlin suger:
private fun isSomePackageInstalled(context: Context, packageName: String): Boolean {
val packageManager = context.packageManager
return runCatching { packageManager.getPackageInfo(packageName, 0) }.isSuccess
}
You can do it using Kotlin extensions :
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)
}
In Kotlin, the simplest way can be two steps
1- in the Manifest put the target app id . ex (com.src.turkey)
<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- In an Activity
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")
}
There isn't any deprecated such as
queryIntentActivities
pm.getPackageInfo

Categories

Resources