How to get Custom SW version number of android - android

I have a Marshmallow device. It has custom built SW.
I have seen the build.prop file. There I can perceive the Software version name comes from ro.custom.build.version
My question is - how can I get the "ro.custom.build.version" information programmatically in my application ?

I got the answer. It is very simple -
First, I created a function to read from SystemProperties.
public String getSystemProperty(String key) {
String value = null;
try {
value = (String) Class.forName("android.os.SystemProperties")
.getMethod("get", String.class).invoke(null, key);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
And then called the function with "ro.custom.build.version" as a key
getSystemProperty("ro.custom.build.version");
Special thanks to #sasikumar for giving me the hint

You have to do as below :
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;
To get the Version code, Use as below :
int verCode = pInfo.versionCode;

Check android.os.Build.VERSION. You can use INCREMENTAL.

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 tell if two themes are equal?

I want some logic around a part of my code that can tell if I am using a certain theme. I would like to be able to tell if the current theme is equal to the theme with parent="Theme.Sherlock.NoActionBar".
I am wondering what the most efficient way to do this is as I can't figure out a simple way.
You can use the following code to get the name of the Theme and then compare it with the one you want to check.
public String returnThemeName()
{
PackageInfo packageInfo;
try
{
packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
int themeResId = packageInfo.applicationInfo.theme;
return getResources().getResourceEntryName(themeResId);
}
catch (NameNotFoundException e)
{
return null;
}
}

Get Application Directory

Does someone know how do I get the path of my application directory? (e.g. /data/data/my.app.lication/)
Currently I'm using this method: myActivity.getFilesDir().getParent(); but it seems to me like a workaround when there's a simpler solution. Also, the side-effect is the creation of the files directory, which is un-needed.
Clarification: First - Thanks for the repliers. I try to understand if there's already exists method that does it, not for another work-around.
There is a simpler way to get the application data directory with min API 4+. From any Context (e.g. Activity, Application):
getApplicationInfo().dataDir
http://developer.android.com/reference/android/content/Context.html#getApplicationInfo()
PackageManager m = getPackageManager();
String s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
If eclipse worries about an uncaught NameNotFoundException, you can use:
PackageManager m = getPackageManager();
String s = getPackageName();
try {
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
} catch (PackageManager.NameNotFoundException e) {
Log.w("yourtag", "Error Package name not found ", e);
}
Just use this in your code
context.getApplicationInfo().dataDir
I got this
String appPath = App.getApp().getApplicationContext().getFilesDir().getAbsolutePath();
from here:
For current Android application package:
public String getDataDir(Context context) throws Exception {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir;
}
For any package:
public String getAnyDataDir(Context context, String packageName) throws Exception {
return context.getPackageManager().getPackageInfo(packageName, 0).applicationInfo.dataDir;
}
If you're trying to get access to a file, try the openFileOutput() and openFileInput() methods as described here. They automatically open input/output streams to the specified file in internal memory. This allows you to bypass the directory and File objects altogether which is a pretty clean solution.
Based on #jared-burrows' solution. For any package, but passing Context as parameter...
public static String getDataDir(Context context) throws Exception {
return context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0)
.applicationInfo.dataDir;
}

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;
}
}

Finding app version number in code

Is there a simple way to find the current version of my application from within it? I would like to add the version string to the about screen.
You should check this question.
I am doing it this way:
try {
String pkg = mContext.getPackageName();
mVersionNumber = mContext.getPackageManager().getPackageInfo(pkg, 0).versionName;
} catch (NameNotFoundException e) {
mVersionNumber = "?";
}
please try out this.
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
version = pInfo.versionName;

Categories

Resources