反射获取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); } }
...全文
137 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
旧I恋 2019-03-04
  • 打赏
  • 举报
回复
修正@wzkacyw的代码,谢谢😜

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧