Detect an app installed [duplicate] - android

This question already has an answer here:
Identifying if an app exists, if not go to play store
(1 answer)
Closed 9 years ago.
Could I know If there is a specific app installed in the phone programmatically? My app would make a process in one way or in other depending on that. How could I get this information?
Thanks!!

private boolean isAppInstalled(String packageName) {
PackageManager pm = getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}

You must know the package name to check weather the application is installed or not
say if package name is like com.myapp.name
use the following to existence
boolean isExists;
try
{
getPackageManager().getPackageInfo("com.myapp.name", PackageManager.GET_ACTIVITIES);
isExists = true;
}
catch(NameNotFoundException e)
{
isExists = false;
// Sep 11, 2013 8:39:47 PM
Log.e("Exception", "NameNotFoundException" + String.valueOf(e.getMessage()));
e.printStackTrace();
}
Must use try catch around to handling expected error when NameNotFoundException is thrown

Related

How to detect installed Chrome version from my Android App?

I am working on a feature where I need to transition user from Android Native APP to Chrome custom tab only if chrome version installed on the device is greater than version 65. So my question is, is there a way we can detect chrome version from Android Native App ?
private boolean isChromeInstalledAndVersionGreaterThan65() {
PackageInfo pInfo;
try {
pInfo = getPackageManager().getPackageInfo("com.android.chrome", 0);
} catch (PackageManager.NameNotFoundException e) {
//chrome is not installed on the device
return false;
}
if (pInfo != null) {
//Chrome has versions like 68.0.3440.91, we need to find the major version
//using the first dot we find in the string
int firstDotIndex = pInfo.versionName.indexOf(".");
//take only the number before the first dot excluding the dot itself
String majorVersion = pInfo.versionName.substring(0, firstDotIndex);
return Integer.parseInt(majorVersion) > 65;
}
return false;
}
Obviously this will work until Chrome will be versioned how they did until now, if they will decide to change the versioning the logic should be updated as well. (I don't think this will happen but for max safety put everything in a try-catch)
private boolean checkVersion(String uri) {
String versionName;
int version = 0;
PackageManager pm = getPackageManager();
try {
versionName = pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES).versionName;
String[] split = versionName.split(Pattern.quote("."));
String major = split[0];
version = Integer.parseInt(major);
return version > 65;
} catch (PackageManager.NameNotFoundException e) {
//Catch the Exception
}
return false;
}
Then call above method using
checkVersion("com.android.chrome"); //This will return a boolean value

How to check if the app is game or not programmatically? [duplicate]

This question already has answers here:
How to detect if an android app is a game?
(3 answers)
Closed 7 years ago.
I am planning to make an android app for controlling my phone usage by detecting if an playstore-installed app is game or not. So if the installed app is a game app, my app would detect that the installed app is a kind of game and would not allow the game app to run.
I wonder if there is any source code for this.
There is a way to check this since API level 21 and it has recently changed in API level 26. These are the correct backwards compatible ways to do this.
Java:
public static boolean packageIsGame(Context context, String packageName) {
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return info.category == ApplicationInfo.CATEGORY_GAME;
} else {
// We are suppressing deprecation since there are no other options in this API Level
//noinspection deprecation
return (info.flags & ApplicationInfo.FLAG_IS_GAME) == ApplicationInfo.FLAG_IS_GAME;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("Util", "Package info not found for name: " + packageName, e);
// Or throw an exception if you want
return false;
}
}
Kotlin:
fun packageIsGame(context: Context, packageName: String): Boolean {
return try {
val info: ApplicationInfo = context.packageManager.getApplicationInfo(packageName, 0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
info.category == ApplicationInfo.CATEGORY_GAME
} else {
// We are suppressing deprecation since there are no other options in this API Level
#Suppress("DEPRECATION")
(info.flags and ApplicationInfo.FLAG_IS_GAME) == ApplicationInfo.FLAG_IS_GAME
}
} catch (e: PackageManager.NameNotFoundException) {
Log.e("Util", "Package info not found for name: " + packageName, e)
// Or throw an exception if you want
false
}
}
Source: Android Documentation

How to get current version of my Android app in market ? I just want to check version

e.g If user clicks on check for update then action should be..
Check version of my application in market
If my application's current version and market version are not same then I just want to give a dialog of "Upgrade now".
Also wants to know is there any possibilities without WebServices for this aciton? because I have referred some answers with WebServices, instead of it is there any possibilities then please share your knowledge.
Thanks in advance :)
You can't get version of android market app, I wanna explain this... You can get current version of the app with this code:
public int getAppVersion(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
// should never happen
throw new RuntimeException("Could not get package name: " + e);
}
}
If you want to know the version that have android market, that it's impossible, you need to think alternative, webservice how you say... In my case, for example, I save the version of Android Market in my server, when I sync my app with my server I obtain the AndroidMarket version then in this moment I can compare versions and apply the necessary code, in your case showDialog.
Not exist alternative that you can get version from you android app market, why? The apps have their code and not it's the same code for all apps...
Tell me if I helped you and good programming!
public String getLatestVersionNumber()
{
String versionNumber = "0.0.0";
try
{
Document doc = Jsoup.connect("<market:urlofparticularapp>").get();
Elements changeLog = doc.select("div.clDesc");
for (Element div : changeLog)
{
String divText = div.text();
if (divText.contains("Version"))
{
return divText.split(" ")[1];
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return versionNumber;
}
Download Library of Jsoup from http://jsoup.org/download
As I know there is no way to get current version of market Application.
But i am doing this in my application by using of server.
I am putting the Upcoming app version on server and getting the current app version from app By using this code
oldVersion = Splash.this.getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
When app will launch then we will get the version from server and compare with app version for old user. If version will mismatch then give the popup for update for application using this code
Uri uri = Uri.parse("market://details?id=" + MainActivity.this.getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
MainActivity.this.startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
MainActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + MainActivity.this.getPackageName())));
}

How can i get the apk file name and path programmatically?

I want to get the exact file name of a program if I already know the package name of the target apk. For instance, if I know the package name of my apk, which is com.packagename, how can I get the exact path and file name of that package? Btw, i don't want to get just MY apk location, i want the location of any package name i apply. SystemTuner pro is able to do this so i know it is possible, just not sure how.
Thanks guys!
/**
* Get the apk path of this application.
* #param context any context (e.g. an Activity or a Service)
* #return full apk file path, or null if an exception happened (it should not happen)
*/
public static String getApkName(Context context) {
String packageName = context.getPackageName();
PackageManager pm = context.getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
String apk = ai.publicSourceDir;
return apk;
} catch (Throwable x) {
}
return null;
}
EDIT
In defense of catch (Throwable x) in this case. At first, now it is well-known that Checked Exceptions are Evil. At second, you cannot predict what may happen in future versions of Android. There already is a trend to wrap checked exceptions into runtime exceptions and re-throw them. (And a trend to do silly things that were unthinkable in the past.) As to the children of Error, well, if the package manager cannot find the apk that is running, it is the kind of problems for which Errors are thrown. Probably the last lines could be
} catch (Throwable x) {
return null;
}
but I do not change working code without testing it.
PackageManager.getPackageInfo() returns information about the package, and PackageInfo.applicationInfo field has required information about the application.
Well, i would like to mark Yuri as the answer but i already knew about that stuff. So I went through each and every option from PackageManager.ApplicationInfo and found .publicSourceDir
So a complete answer with code to my question would be
PackageManager pm = getPackageManager();
try {
ApplicationInfo ai = pInfo.getApplicationInfo(<packageName here>, 0);
String sourceApk = ai.publicSourceDir;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
So thanks again guys, got my brain goin once again Love StackOverflow!
in above answer need change pInfo to pm
like this
PackageManager pm = getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(<packageName here>, 0);
String sourceApk = ai.publicSourceDir;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this answer by Seth

How do I get the version number of an application in Android?

We have a set of 3-5 android applications that we have developed for an enterprise to integrate with our back-end. How do we create an installer system that upgrades applications automatically. We were thinking of getting version numbers and querying the backend to get current versions and downloading them.
How do I get the version number of an application in Android?
ApplicationInfo info = getApplicationInfo();
try {
info = getPackageManager().getApplicationInfo(info.packageName, PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Any pointers will be most useful.
Thanks
Sameer
Using the function below you can get the current Version Name or No for the application.
This you can check against that of the app at server side and if needed you can upgrade app.
public static function String getVersionName(Context context, Class cls) {
try {
ComponentName comp = new ComponentName(context, cls);
PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
return pinfo.versionName;
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
return null;
}
}

Categories

Resources