这里写代码片最近公司适配华为p9手机,系统是7.0出现了两个内存泄露的问题
解决办法,在activity的ondestory中加入两个方法即可

 public void fixHuaWeiMemoryLeak() {if (!"HUAWEI".equals(Build.MANUFACTURER)) {return;}try {Class<?> GestureBoostManagerClass = Class.forName("android.gestureboost.GestureBoostManager");Field sGestureBoostManagerField = GestureBoostManagerClass.getDeclaredField("sGestureBoostManager");sGestureBoostManagerField.setAccessible(true);Object gestureBoostManager = sGestureBoostManagerField.get(GestureBoostManagerClass);Field contextField = GestureBoostManagerClass.getDeclaredField("mContext");contextField.setAccessible(true);if (contextField.get(gestureBoostManager) != null) {contextField.set(gestureBoostManager, null);}} catch (Throwable t) {}}@RequiresApi(api = Build.VERSION_CODES.KITKAT)public  void fixFocusedViewLeak(Application application) {// Don't know about other versions yet.if (Build.VERSION.SDK_INT > 23) {return;}final InputMethodManager inputMethodManager =(InputMethodManager) application.getSystemService(Context.INPUT_METHOD_SERVICE);final Field mServedViewField;final Field mHField;final Method finishInputLockedMethod;final Method focusInMethod;try {mServedViewField = InputMethodManager.class.getDeclaredField("mServedView");mServedViewField.setAccessible(true);mHField = InputMethodManager.class.getDeclaredField("mServedView");mHField.setAccessible(true);finishInputLockedMethod = InputMethodManager.class.getDeclaredMethod("finishInputLocked");finishInputLockedMethod.setAccessible(true);focusInMethod = InputMethodManager.class.getDeclaredMethod("focusIn", View.class);focusInMethod.setAccessible(true);} catch (NoSuchMethodException | NoSuchFieldException unexpected) {Log.e("IMMLeaks", "Unexpected reflection exception", unexpected);return;}application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {@Overridepublic void onActivityDestroyed(Activity activity){}@Overridepublic void onActivityStarted(Activity activity){}@Overridepublic void onActivityResumed(Activity activity){}@Overridepublic void onActivityPaused(Activity activity){}@Overridepublic void onActivityStopped(Activity activity){}@Overridepublic void onActivitySaveInstanceState(Activity activity, Bundle bundle){}@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {ReferenceCleaner cleaner = new ReferenceCleaner(inputMethodManager, mHField, mServedViewField,finishInputLockedMethod);View rootView = activity.getWindow().getDecorView().getRootView();ViewTreeObserver viewTreeObserver = rootView.getViewTreeObserver();viewTreeObserver.addOnGlobalFocusChangeListener(cleaner);}});}static class ReferenceCleaner implements MessageQueue.IdleHandler, View.OnAttachStateChangeListener,ViewTreeObserver.OnGlobalFocusChangeListener {private final InputMethodManager inputMethodManager;private final Field mHField;private final Field mServedViewField;private final Method finishInputLockedMethod;ReferenceCleaner(InputMethodManager inputMethodManager, Field mHField, Field mServedViewField,Method finishInputLockedMethod) {this.inputMethodManager = inputMethodManager;this.mHField = mHField;this.mServedViewField = mServedViewField;this.finishInputLockedMethod = finishInputLockedMethod;}@Override public void onGlobalFocusChanged(View oldFocus, View newFocus) {if (newFocus == null) {return;}if (oldFocus != null) {oldFocus.removeOnAttachStateChangeListener(this);}Looper.myQueue().removeIdleHandler(this);newFocus.addOnAttachStateChangeListener(this);}@Override public void onViewAttachedToWindow(View v) {}@Override public void onViewDetachedFromWindow(View v) {v.removeOnAttachStateChangeListener(this);Looper.myQueue().removeIdleHandler(this);Looper.myQueue().addIdleHandler(this);}@Override public boolean queueIdle() {clearInputMethodManagerLeak();return false;}@RequiresApi(api = Build.VERSION_CODES.KITKAT)private void clearInputMethodManagerLeak() {try {Object lock = mHField.get(inputMethodManager);// This is highly dependent on the InputMethodManager implementation.synchronized (lock) {View servedView = (View) mServedViewField.get(inputMethodManager);if (servedView != null) {boolean servedViewAttached = servedView.getWindowVisibility() != View.GONE;if (servedViewAttached) {// The view held by the IMM was replaced without a global focus change. Let's make// sure we get notified when that view detaches.// Avoid double registration.servedView.removeOnAttachStateChangeListener(this);servedView.addOnAttachStateChangeListener(this);} else {// servedView is not attached. InputMethodManager is being stupid!Activity activity = extractActivity(servedView.getContext());if (activity == null || activity.getWindow() == null) {// Unlikely case. Let's finish the input anyways.finishInputLockedMethod.invoke(inputMethodManager);} else {View decorView = activity.getWindow().peekDecorView();boolean windowAttached = decorView.getWindowVisibility() != View.GONE;if (!windowAttached) {finishInputLockedMethod.invoke(inputMethodManager);} else {decorView.requestFocusFromTouch();}}}}}} catch (IllegalAccessException |InvocationTargetException unexpected) {Log.e("IMMLeaks", "Unexpected reflection exception", unexpected);}}private Activity extractActivity(Context context) {while (true) {if (context instanceof Application) {return null;} else if (context instanceof Activity) {return (Activity) context;} else if (context instanceof ContextWrapper) {Context baseContext = ((ContextWrapper) context).getBaseContext();// Prevent Stack Overflow.if (baseContext == context) {return null;}context = baseContext;} else {return null;}}}}

解决华为手机InputMethodManager和GestureBoostManager的内存泄露的问题相关推荐

  1. Android:解决华为手机隐藏虚拟按键Activity被重新创建的问题

    解决华为手机隐藏虚拟按键Activity被重新创建的问题 问题描述 解决方法 分析 问题描述 在华为手机P9上 屏幕底部虚拟按键用户可以随时隐藏或显示,在改变后 返回上一页,会导致页面重新创建,页面操 ...

  2. Android 头像替换,解决华为手机取不到图片

    只用涉及到用户模块的App基本上就会用到头像替换的功能,类似的代码也是信手沾来,百度.GitHub以及各大论坛好博客一大把,随便粘过来就可以用了.但是...有坑.在华为荣耀手机上踩坑了,网上看了下问的 ...

  3. 如何阻止华为杀应用_如何解决华为手机“杀后台”严重的情况呢?小技巧就能轻松搞定...

    大家好,我是鲍清,这里专注数码,相信有很多人都喜欢,小编来给你们推荐,大家可以收藏转发哦! iOS13.2Beta版本因为杀后台严重被我们诟病不已,而华为确实也存在这样的问题,可是华为这样做" ...

  4. android手机存储速度慢,安卓内存泄露后台应用被迫关闭、系统速度慢的解决方法...

    安卓内存泄露怎么办?升级到lollipop的android设备用户们有没有遇到后台应用被迫关闭.系统速度慢.经常重启等情况呢?其实这是内存泄露问题,下文小乐哥分享安卓内存泄露解决办法,遇到朋友可以节操 ...

  5. sqlserver2000内存突破4g_友商都上16G,华为手机为何一直用8G内存?网友:效仿iOS...

    相信大家都能感受到,安卓手机的内存在不断攀升,从以前的512M,发展到现在的16G.三星S20 Ultra.努比亚红魔5G.联想拯救者电竞手机Pro都推出了采用16G的内存的版本,估计明年会更多搭载1 ...

  6. 解决华为手机EMUI 9之后,GMS无法使用问题

    chrome,goolgle play,youtube 这几个经常用的app,在我某一天升级我的华为手机到emui9之后突然用不了了,一直试图解决,找了一堆gms安装器,都不好使.又不想破解boot ...

  7. 解决华为手机隐藏虚拟按键Activity被重启的问题

    引言 最近技术群里面的妹纸说:在华为手机上开发应用的时候,隐藏底部的虚拟按键Activity会被重启的问题,并且找不到问题所在,于是查查资料,发现问题解决起来蛮简单的. 问题描述 一开始觉得应该是代码 ...

  8. 解决华为手机无法输出Debug级别log的问题

    近期购入了新款的华为手机荣耀8,手感.性能.颜值都非常好.作为android开发工程师,自然会用到真机进行日常的调试.然而,这部手机并没有这么"听话"!反复尝试开启开发者选项中的设 ...

  9. 解决华为手机破解巧影软件无法使用

    原文地址:https://blog.csdn.net/dx147305114/article/details/87863052 本次解决的是部分巧影破解软件的问题,在网上看了很多都是关于华为手机无法使 ...

最新文章

  1. 使用Junit4时问题
  2. access database in a helper function ?
  3. Spring Boot 中关于 %2e 的 Trick
  4. 【鸿蒙 HarmonyOS】UI 组件 ( 多选按钮 | Checkbox 组件 )
  5. Alamofire的简单封装
  6. 11134 - Fabled Rooks
  7. java混淆工具对比,java-混淆如何在另一个类中键入比较器
  8. Azure database
  9. 【元胞自动机】基于matlab元胞自动机考虑驾驶行为的自动—求解手动驾驶混合交通流问题【含Matlab源码 2060期】
  10. Android arm64-v8a、armeabi-v7a、armeabi、x86详解
  11. 台式计算机把光驱改成硬盘,笔记本电脑光驱怎么改换硬盘?笔记本光驱改装固态硬盘图文教程...
  12. 电脑只能上QQ不能上网怎么办
  13. c语言某天星期几,计算某天是星期几【C代码】
  14. 新浪开放平台:解决获取access_token抛 21323 异常,以及接口调用
  15. php几个时间段去除重复,一个时间段内各地区数据和,发现重复地区不相加
  16. CAD如何创建图层并绘制图形
  17. 用Word2003打开.docx文档
  18. requests库爬取百度首页
  19. 查看oracle执行计划方法( 一)
  20. Spring Cloud-Alibaba

热门文章

  1. VMware vSphere 7.0 发行说明文档
  2. 第三人称和第一人称互相切换【Low版】
  3. 从程序中学习UKF-SLAM(二)
  4. 【数据结构】二叉树遍历
  5. Library projects cannot set applicationId问题解决
  6. AAAI 2023 | 腾讯优图实验室16篇论文入选,含多标签分类、姿态估计、目标检测、HOI、小样本学习等研究方向...
  7. 《商业模式》读书笔记
  8. 差评回复话术,拿来吧你!
  9. 处理亚马逊差评的几个方法
  10. Tomcat - 解决which must be escaped when used within the value错误