高版本编译在低版本是可以用的,只要minsdk支持,低版本编译的在高版本不可用。
系统服务类的反射方法 注册监听和解注册
也可参考参考https://www.cnblogs.com/garfieldcgf/p/6149285.html
public static int callGetxxdDisplayId(Context context) {
int xxid;
try {
Class<?> clazz = Class.forName("android.multidisplay.xxManager");
String serverName = context.getSystemServiceName(clazz);
Object server = context.getSystemService(serverName);
Method method = clazz.getMethod("getxxmethod");
method.setAccessible(true);
xxid = (int) method.invoke(server);
return xxid;
} catch (Exception e) {
e.printStackTrace();
}
return Display.DEFAULT_DISPLAY;
}
public static void registerDisplayListener(Context context,InvocationHandler mHandler) {
try {
Class<?> clazz = Class.forName("android.xxdisplay.xxdisplayManager");
String serverName = context.getSystemServiceName(clazz);
Object server = context.getSystemService(serverName);
Class<?> mListener = Class.forName("android.xxdisplay.xxdisplayManager$FocusDisplayListener");
Object mObj = Proxy.newProxyInstance(ReflectUtils.class.getClassLoader(),new Class[] { mListener }, mHandler);
Method mMethod = clazz.getDeclaredMethod("registerFocusDisplayListener", new Class[] { mListener });
mMethod.setAccessible(true);
mMethod.invoke(server, new Object[] { mObj });
} catch (Exception e) {
e.printStackTrace();
}
}
public static void unregisterDisplayListener(Context context,InvocationHandler mHandler) {
try {
Class<?> clazz = Class.forName("android.xxdisplay.xxdisplayManager");
String serverName = context.getSystemServiceName(clazz);
Object server = context.getSystemService(serverName);
Class<?> mListener = Class.forName("android.xxdisplay.xxdisplayManager$FocusDisplayListener");
Object mObj = Proxy.newProxyInstance(ReflectUtils.class.getClassLoader(),new Class[] { mListener }, mHandler);
Method mMethod = clazz.getDeclaredMethod("unregisterFocusDisplayListener", new Class[] { mListener });
mMethod.setAccessible(true);
mMethod.invoke(server, new Object[] { mObj });
} catch (Exception e) {
e.printStackTrace();
}
}