当前位置: 首页 > news >正文

Android系统去掉长按webview界面弹框中的分享功能

提示: 最近有个客户自己做了WebView 加载HTML的网页,长按WebView 网页后显示了菜单选项,其中一个就是分享功能。让把它去掉,后面又让把所有的分析菜单去掉

文章目录

  • 前言
  • 一、需求
  • 二、参考文档
  • 三、修改文件
  • 四、效果演示
  • 五、实现方案
    • 实现方案
    • 实现思路推荐
  • 六、代码跟踪
    • 1、找到浮动组件-frameworks/base/core/java/com/android/internal/widget/floatingtoolbar
    • 2、floatingtoolbar 包下类架构分析
      • FloatingToolbarPopup 定义接口
      • FloatingToolbar 水平包裹的工具类-负责展示menu items
      • LocalFloatingToolbarPopup 负责实际 popup window的工具类-对用户展示
    • 3 、FloatingToolbar 源码分析
      • 查看 `doShow` `getVisibleAndEnabledMenuItems` 方法如下
      • 查看日志打印
    • 4 、 查找资源文件-过滤操作
  • 总结

前言

一、需求

Android 系统去解决长按显示内容,去掉分享功能,不论那个界面,是所有界面都需要满足。

需求:

  • 如果仅仅去掉系统自带的,那么系统来处理
  • 如果客户自定义,部分App自定义的菜单项,不是调用系统的,那么还是需要应用去解决的。
  • 之前做过Editext,其实还需要去TextView,最难点的就是WebView中的HTML了 去分享功能了。

如下:WebView中涉及到分享功能菜单的界面,

二、参考文档

自己之前的笔记
RK-3576-Android15屏蔽EditText的长按分享菜单的功能 涉及到另外一个组件TextView 的屏蔽 功能,和Editext 修改方案一模一样。

其它参考资料比较有建设性,但是要付费: 参考下,然后自己分析下代码实现呗,但是千万不要相信别人的建议,实际效果都需要自己不断查阅资料、定位源码、分析源码、反复实践调试 才行。

Android 10.0 framework去掉长按webview界面弹框中的 打开 字符串
Android 14.0 framework去掉长按webview界面弹框中的打开字符串

三、修改文件

frameworks/base/core/java/com/android/internal/widget/floatingtoolbar/FloatingToolbar.java

四、效果演示


五、实现方案

实现方案

frameworks/base/core/java/com/android/internal/widget/floatingtoolbar/FloatingToolbar.java中的getVisibleAndEnabledMenuItems方法中,过滤掉分享菜单即可。

实际代码改成如下:

/** * Returns the visible and enabled menu items in the specified menu. * This method is recursive. */privatestaticList<MenuItem>getVisibleAndEnabledMenuItems(Menumenu){Log.d(TAG,"==========getVisibleAndEnabledMenuItems==========");List<MenuItem>menuItems=newArrayList<>();for(inti=0;(menu!=null)&&(i<menu.size());i++){MenuItemmenuItem=menu.getItem(i);if(menuItem.isVisible()&&menuItem.isEnabled()){MenusubMenu=menuItem.getSubMenu();if(subMenu!=null){Log.d(TAG,"=====menuItems.addAll(getVisibleAndEnabledMenuItems(subMenu))=======");menuItems.addAll(getVisibleAndEnabledMenuItems(subMenu));}else{Log.d(TAG,"==========getVisibleAndEnabledMenuItems=== menuItems.add(menuItem)======="+menuItem.getTitle()+" "+menuItem.getTooltipText());if(menuItem.getTitle().equals(mTWindow.getContext().getResources().getString(com.android.internal.R.string.share))){Log.d(TAG,"==========now is share not add menuItem====");continue;}menuItems.add(menuItem);}}}returnmenuItems;}

实现思路推荐

  • dumsys堆栈打印,查找难找的组件信息dumpsys window
  • 查找share 相关的ID、字符串信息
  • 通过\frameworks\base\core\res\res中的values,查找关键字,反向grep查找
  • 网上多看看资料吧FloatingToolbar

六、代码跟踪

1、找到浮动组件-frameworks/base/core/java/com/android/internal/widget/floatingtoolbar

相关说明:

  • 在 RK-3576-Android15屏蔽EditText的长按分享菜单的功能 笔记中 有了解过,系统的基本组件都在frameworks/base/core/java/com/android/internal/widget目录下 所以我们最开始的思路 就应该在这个包名下面找对应的基本组件,但是我们遇到第一个问题,没法像之前那样 通过 关键字段、关键方法 搜索到对应相关信息。
  • 大家看实际效果,是不是很像poppupwindows,实际打印堆栈信息,真的好多Windows相关的信息,floatingtoolbar就是一个浮动组件,实现了这个菜单功能。

2、floatingtoolbar 包下类架构分析

当我分析到这里的时候,我就感觉找到位置了,分析代码,然后更改即可实现需求

FloatingToolbarPopup 定义接口

/** * A popup window used by the {@link FloatingToolbar} to render menu items. * */publicinterfaceFloatingToolbarPopup{/** * Sets the suggested dp width of this floating toolbar. * The actual width will be about this size but there are no guarantees that it will be exactly * the suggested width. */voidsetSuggestedWidth(intsuggestedWidth);/** * Sets if the floating toolbar width changed. */voidsetWidthChanged(booleanwidthChanged);/** * Shows this popup at the specified coordinates. * The specified coordinates may be adjusted to make sure the popup is entirely on-screen. */voidshow(List<MenuItem>menuItems,MenuItem.OnMenuItemClickListenermenuItemClickListener,RectcontentRect);/** * Gets rid of this popup. If the popup isn't currently showing, this will be a no-op. */voiddismiss();/** * Hides this popup. This is a no-op if this popup is not showing. * Use {@link #isHidden()} to distinguish between a hidden and a dismissed popup. */voidhide();/** * Returns {@code true} if this popup is currently showing. {@code false} otherwise. */booleanisShowing();/** * Returns {@code true} if this popup is currently hidden. {@code false} otherwise. */booleanisHidden();/** * Makes this toolbar "outside touchable" and sets the onDismissListener. * * @param outsideTouchable if true, the popup will be made "outside touchable" and * "non focusable". The reverse will happen if false. * @param onDismiss * * @return true if the "outsideTouchable" setting was modified. Otherwise returns false * * @see PopupWindow#setOutsideTouchable(boolean) * @see PopupWindow#setFocusable(boolean) * @see PopupWindow.OnDismissListener */booleansetOutsideTouchable(booleanoutsideTouchable,@NullablePopupWindow.OnDismissListeneronDismiss);/** * Returns {@link LocalFloatingToolbarPopup} implementation. */staticFloatingToolbarPopupcreateInstance(Contextcontext,Viewparent){returnnewLocalFloatingToolbarPopup(context,parent);}}

FloatingToolbar 水平包裹的工具类-负责展示menu items

/** * A floating toolbar for showing contextual menu items. * This view shows as many menu item buttons as can fit in the horizontal toolbar and the * the remaining menu items in a vertical overflow view when the overflow button is clicked. * The horizontal toolbar morphs into the vertical overflow view. */publicfinalclassFloatingToolbar{

LocalFloatingToolbarPopup 负责实际 popup window的工具类-对用户展示

/** * A popup window used by the floating toolbar to render menu items in the local app process. * * This class is responsible for the rendering/animation of the floating toolbar. * It holds 2 panels (i.e. main panel and overflow panel) and an overflow button * to transition between panels. */publicfinalclassLocalFloatingToolbarPopupimplementsFloatingToolbarPopup{

3 、FloatingToolbar 源码分析

先观察类的一些方法,有个直观感受,然后打印日志,方便查找问题。

查看doShowgetVisibleAndEnabledMenuItems方法如下

所以去打印部分信息

查看日志打印

进一步验证了,就是在这里添加的,那么过滤掉就可以了

4 、 查找资源文件-过滤操作

这里有两部分工作

  • 查找资源文件 分享的字符串 表示
  • 字符串在Framework中的获取,来实现匹配 过滤操作
java/android/widget/TextView.java:14238:getResources().getString(com.android.internal.R.string.share)));java/android/widget/Editor.java:3314:com.android.internal.R.string.share)java/android/widget/Editor.java:3372:com.android.internal.R.string.share)java/android/widget/Editor.java:4776:com.android.internal.R.string.share)java/android/widget/ShareActionProvider.java:179:R.string.shareactionprovider_share_with_application);java/android/widget/ShareActionProvider.java:181:R.string.shareactionprovider_share_with);

但是 我们匹配的是字符串,所有要不 字符串id 转字符串表示,然后匹配再过滤。

总结

  • 遇到难找的界面、难找的组件、难定位的UI 直接dumpsys 堆栈找找看
  • 多分析应该在哪里找源码、然后多查阅资料
  • grep 查找、关键字查找、找关键信息
  • 实实在在多看看源码、并实际调试吧
http://www.hn-smt.com/news/95751/

相关文章:

  • 力扣两数之和
  • 跳出 “文献堆砌” 陷阱:paperxieAI 文献综述功能如何帮你搭建学术写作的 “逻辑骨架”?
  • NotchDrop:MacBook刘海区域终极文件管理解决方案
  • Pock:终极MacBook Touch Bar管理器,让你的效率翻倍!
  • 3.MySQL 数据库集成
  • Librum电子书阅读器完整使用教程:打造个人数字图书馆
  • 25、技术探索:从数据查询到包管理的实用指南
  • 哪款系统在生成各种生产报表(如日报、月报)方面比较强大?——制造企业全流程决策指南
  • “医疗专业应用+分布式数据底座”:平凯数据库与金唐软件全链路赋能医疗国产化与数字化转型
  • 2025效率革命:Qwen3-8B-MLX-8bit双模式切换重塑边缘AI部署范式
  • ggplot2终极指南:快速掌握数据可视化的完整安装配置方法
  • 降本增效管理干货:双卧轴混凝土搅拌机核心部件维护技术手册!
  • 深入解析:RabbitMQ-Exporter 监控 TLS 加密的 RabbitMQ 集群
  • 2025年12月米粉机厂家推荐:五大品牌深度评测与性能对比排行榜 - 十大品牌推荐
  • 手把手教学:Win11无法访问共享文件夹?0x8007003
  • Lonsdor K518 Pro FCV TATA License Activation: Unlock TATA Key Programming for Shops Owners
  • 让gemini3做的网页拥有支付功能,访客变付费用户!附提示词
  • 把内部表当成一张小型数据库:ABAP 内部表 Secondary Key 的一次真正落地
  • 如何让AI输出结构化数据:eino框架的终极解析指南
  • 34、网络连接测试与安全防护指南
  • 2025年AI工程师认证报考指南:上海站最新流程
  • 基于Java+ vue智慧医药系统(源码+数据库+文档)
  • Burp Repeater模块
  • 61、Python CGI编程与替代方案全解析
  • Wan2.1-I2V终极指南:简单三步开启AI图生视频新纪元
  • ComfyUI商业案例:电商产品图生成实战
  • My first blog
  • 28、Red Hat Linux:系统管理、应用操作与安全防护全解析
  • CloudMapper完整指南:5步实现AWS安全可视化与自动化审计
  • 2025年质量好的共享智慧空压站厂家推荐及选择指南 - 品牌宣传支持者