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;
}
}
Related
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.
I am developing an app which will contain a list of Apps. On click the user will be redirected to the Play Store to download this app. On successful download I have to send that apps package name to a server to validate it. How can I do that?
I assume you want to do this at runtime, so your app can read its own package_id w/o having this hardcoded. For that you need to use PackageManager's getPackageInfo() method:
protected String getPackageName() {
try {
PackageInfo packageInfo = getPackageManager.getPackageInfo(getPackageName(), 0);
return packageInfo.applicationInfo.packageName;
} catch (Exception e) {
e.printStacktrace();
}
return null;
}
Use apkanalyzer, its part of the Android studio:
apkanalyzer manifest application-id path/to/apk/file.apk
We can use getTheme() inside an activity to get the current theme. But how I can print the name of this retrieved theme?
There are no method such as getTheme().getName(). I tried using resolveAttribute and obtainStyledAttributes, with no success.
You can do something like this :
int theme = 0;
try
{
String packageName = getClass().getPackage().getName();
PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
theme = packageInfo.applicationInfo.theme;
}
catch (Exception e)
{
e.printStackTrace();
}
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
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;
}
}