反射: http://www.cnblogs.com/Quincy/archive/2011/06/19/2084557.html
用例:当APK里面用到了3.0后新增的API, 在2.X环境下编译不通过。
当调用高版本API时用反射可以解决此问题。
public class IVersionDrmApdater implements DrmManagerAdapter
{
private Class<?> drmClass;
private Object mDrmInstance;
private static final String CLAZZ_NAME = "android.drm.DrmManagerClient";
public IVersionDrmApdater(Context c)
{
mContext = c;
try
{
drmClass = Class.forName(CLAZZ_NAME);
@SuppressWarnings("unchecked")
Constructor<Object> constructor = (Constructor<Object>) drmClass.getConstructor(Context.class);
constructor.setAccessible(true);
mDrmInstance = constructor.newInstance(mContext);
} catch (Exception e)
{
e.printStackTrace();
}
}
private int checkRightsStatus(String path, int action)
{
try
{
Method m = drmClass.getMethod("checkRightsStatus", String.class, int.class);
m.setAccessible(true);
return ((Integer) m.invoke(mDrmInstance, path, action)).intValue();
} catch (Exception e)
{
e.printStackTrace();
return DrmStore.Action.DEFAULT;
}
}
}