Android复制粘贴剪切板内容的一种方法
1 Clipper介绍
Clipper是一款简单的app, 它可以通过一行adb shell命令来和安卓系统剪切板服务交互。
官方说明:https://github.com/majido/clipper
2 App下载
下载地址:clipper.apk
3 使用方法
安装App
启动广播服务
adb shell am startservice ca.zgrs.clipper/.ClipboardService
get方法:print the value in clipboard into logs
am broadcast -a clipper.get
set方法:sets the clipboard content to the string passed via extra parameter "text"
am broadcast -a clipper.set -e text "this can be pasted now"
4 Appium中使用的一个例子
某个安卓手机先安装Clipper.apk
然后开启广播
//执行shell命令开启android广播服务 Process process = Runtime.getRuntime().exec("adb -s "+udid+" shell am broadcast -a clipper.get"); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String urlStr=""; String line=""; while ((line = br.readLine()) != null) { urlStr+=line; } if(urlStr.contains("result=0")){ Runtime.getRuntime().exec("adb -s "+udid+" shell am startservice ca.zgrs.clipper/.ClipboardService"); }
其中如果有多台手机,udid为事先定义的设备名称
capabilities.setCapability("deviceName", udid);
若某个url信息已在剪切板中,获取页面url地址
private String getUrl() throws IOException{ String url=""; BufferedReader br =null; try{ Process process = Runtime.getRuntime().exec("adb -s "+udid+" shell am broadcast -a clipper.get"); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "utf-8"); br = new BufferedReader(isr); }catch(Exception e){ System.out.println("广播服务出现异常"); } try{ String urlStr=""; String line=""; while ((line = br.readLine()) != null) { urlStr+=line; } String[] s=null; if(urlStr.contains("data=\"")){ s=urlStr.split("data=\""); } else{ System.out.println("请启动广播服务"); } url=s[1].toString().trim().substring(0, s[1].length()-1); }catch(Exception e){ System.out.println("剪切板获取url异常"); } br.close(); return url; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构