Tuesday 29 January 2013

Android checking the Density of the device and Version of an Application.


In order to get Device Density simply call the getScreenDensity() and version using 
getVersionName().

Create a class Utility and define given two static method and that can be access by YourClassname.getScreenDensity(this)

as well as YourClassname.getVersionName(this)

1) Device Density

/**
* Check the device density
*
* @param context
*   is current Activity
*/
public static String getScreenDensity(Context context) {
String denstity = null;
DisplayMetrics metrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
switch (metrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
denstity = "Low";
break;
case DisplayMetrics.DENSITY_MEDIUM:
denstity = "Medium";
break;
case DisplayMetrics.DENSITY_HIGH:
denstity = "High";
break;
default:
denstity = "Ehigh";
break;
}
Log.d(TAG, "getScreenDensity=" + metrics.densityDpi);
return denstity;
}

2) Version Name

/**
* Check the device version mane
*
* @param context
* is current Activity
*/
public static String getVersionName(Context context) {
PackageInfo pinfo;
String versionName = null;
try {
pinfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
versionName = pinfo.versionName;
} catch (NameNotFoundException e) {
versionName = "Name Not Found";
}
return versionName;

}

No comments:

Post a Comment