反射获取IWIndowManager的getRotation
旧I恋 2019-03-04 03:36:06 //int rotation = (int)getRotation.invoke(IWindowManager);用法
Method getRotation ;
Object IWindowManager ;
public void getRotation(){
try {
Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
Object ServiceManager = getServiceMethod.invoke(null, new Object[]{"window"});
Class<?> cStub = Class.forName("android.view.IWindowManager$Stub");
Method asInterface = cStub.getMethod("asInterface", IBinder.class);
IWindowManager = asInterface.invoke(null, ServiceManager);
if(Build.VERSION.SDK_INT < 26)
getRotation = IWindowManager.getClass().getMethod("getRotation");
else
getRotation = IWindowManager.getClass().getMethod("getDefaultDisplayRotation");
} catch (Exception e) {
System.out.println(e);
}
}