Example usage for java.lang.reflect Field getInt

List of usage examples for java.lang.reflect Field getInt

Introduction

In this page you can find the example usage for java.lang.reflect Field getInt.

Prototype

@CallerSensitive
@ForceInline 
public int getInt(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.

Usage

From source file:Main.java

public static Bitmap getAppIcon(Context context) {
    try {//from w w  w . java 2 s. c  om
        Class<?> drawableClass = Class.forName(context.getPackageName() + ".R$drawable");
        Field f = drawableClass.getField("ic_launcher");
        return BitmapFactory.decodeResource(context.getResources(), f.getInt(null));
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public final static int getStaticIntValue(String name, int defvalue) {
    int result = defvalue;
    Field field = getField(name);

    if (field != null) {
        try {// ww w  .  j a  v  a2 s  .c om
            result = field.getInt(null);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static Drawable getDrawableByName(String name, Context context) {
    try {//from  www .  jav a2s. c  o  m
        Field field = Class.forName("com.poetic.emotion.R$drawable").getField(name);
        int drawableRes = field.getInt(field);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return context.getDrawable(drawableRes);
        } else {
            return context.getResources().getDrawable(drawableRes);
        }
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static int getPlatformVersion() {
    try {//from   w ww  . ja  v a 2s . c  om
        Field verField = Class.forName("android.os.Build$VERSION").getField("SDK_INT");
        int ver = verField.getInt(verField);
        return ver;
    } catch (Exception e) {
        try {
            Field verField = Class.forName("android.os.Build$VERSION").getField("SDK");
            String verString = (String) verField.get(verField);
            return Integer.parseInt(verString);
        } catch (Exception ex) {
            return -1;
        }
    }
}

From source file:Main.java

public static int getOpValue(String name) {
    try {/*from   w ww  . jav  a2  s . c  o  m*/
        final Field f = AppOpsManager.class.getField(name);
        f.setAccessible(true);
        return f.getInt(null);
    } catch (IllegalAccessException e) {
        // ignore
    } catch (IllegalArgumentException e) {
        // ignore
    } catch (NoSuchFieldException e) {
        // ignore
    }

    return -1;
}

From source file:Main.java

private static int getImageViewFieldValue(Object object, String fieldName) {
    int value = 0;
    try {//from  w w w  .  j  ava2 s . c  o m
        Field field = ImageView.class.getDeclaredField(fieldName);
        field.setAccessible(true);
        int fieldValue = field.getInt(object);
        if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
            value = fieldValue;
        }
    } catch (Exception e) {
    }
    return value;

}

From source file:Main.java

public static int getPid(Process process) {
    int pid = 0;//w ww.  j av a2s .c o m
    String pname = process.getClass().getName();
    if (pname.equals("java.lang.ProcessManager$ProcessImpl")) {

        try {
            java.lang.reflect.Field f = process.getClass().getDeclaredField("pid");
            f.setAccessible(true);
            pid = f.getInt(process);
        } catch (Throwable e) {
        }
    }
    return pid;
}

From source file:Main.java

public final static int getIntValue(Object owner, String name, int defvalue) {
    int result = defvalue;
    setClass(owner.getClass().getName());
    Field field = getField(name);

    if (field != null) {
        try {//w w w .  j ava  2s. c o  m
            result = field.getInt(owner);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static boolean hasPermission(Context appContext, String appOpsServiceId) throws UnknownError {

    ApplicationInfo appInfo = appContext.getApplicationInfo();

    String pkg = appContext.getPackageName();
    int uid = appInfo.uid;
    Class appOpsClass = null;//from ww w. j av a  2s.  c  om
    Object appOps = appContext.getSystemService("appops");

    try {

        appOpsClass = Class.forName("android.app.AppOpsManager");

        Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
                String.class);

        Field opValue = appOpsClass.getDeclaredField(appOpsServiceId);

        int value = (int) opValue.getInt(Integer.class);
        Object result = checkOpNoThrowMethod.invoke(appOps, value, uid, pkg);

        return Integer.parseInt(result.toString()) == 0; // AppOpsManager.MODE_ALLOWED

    } catch (ClassNotFoundException e) {
        throw new UnknownError("class not found");
    } catch (NoSuchMethodException e) {
        throw new UnknownError("no such method");
    } catch (NoSuchFieldException e) {
        throw new UnknownError("no such field");
    } catch (InvocationTargetException e) {
        throw new UnknownError("invocation target");
    } catch (IllegalAccessException e) {
        throw new UnknownError("illegal access");
    }

}

From source file:Main.java

public static int getFd(FileDescriptor fileDescriptor) {
    int fdInt = -1;
    try {/*  ww  w. j av  a  2  s  .  c  om*/
        if (fileDescriptor != null) {
            Field descriptor = fileDescriptor.getClass().getDeclaredField("descriptor");
            descriptor.setAccessible(true);
            fdInt = descriptor.getInt(fileDescriptor);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return fdInt;
}