1 package js.kbars; 2 3 import android.content.Context; 4 import android.content.pm.PackageManager.NameNotFoundException; 5 import android.util.DisplayMetrics; 6 import android.view.WindowManager; 7 import java.lang.reflect.Field; 8 9 public class Util { logTag(Class<?> c)10 public static String logTag(Class<?> c) { 11 return "kbars." + c.getSimpleName(); 12 } 13 getField(Object obj, String fieldName)14 public static Object getField(Object obj, String fieldName) { 15 Class<?> c = obj.getClass(); 16 try { 17 if (obj instanceof String) { 18 c = c.getClassLoader().loadClass((String) obj); 19 obj = null; 20 } 21 Field f = c.getDeclaredField(fieldName); 22 f.setAccessible(true); 23 return f.get(obj); 24 } catch (Throwable t) { 25 throw new RuntimeException(t); 26 } 27 } 28 getDensityDpi(Context context)29 public static int getDensityDpi(Context context) { 30 DisplayMetrics metrics = new DisplayMetrics(); 31 ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getMetrics(metrics); 32 return metrics.densityDpi; 33 } 34 getVersionName(Context context)35 public static String getVersionName(Context context) { 36 try { 37 return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; 38 } catch (NameNotFoundException e) { 39 throw new RuntimeException(e); 40 } 41 } 42 } 43