服务-android剪贴板监听器 [英] service - android clipboard listener

查看:158
本文介绍了服务-android剪贴板监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的服务(该服务将在后台运行),当用户从浏览器或短信等复制任何内容时,将会有一个吐司显示该文本.

I need a simple service (which will run in the background), when user copies anything from the browser or sms etc., there will be a toast showing that text.

示例:

此服务必须在android 2.1及更高版本上运行.

this service must be run on android 2.1 and later.

今天(从10:35 AM到现在[11:11 PM])我一直在互联网上搜索并测试了一些代码,但是到目前为止,我还没有得出结论.

Today (from 10:35 AM to now[11:11 PM]) I've been searching the internet and tested several codes, but so far I have not come to a conclusion.

一些回答此类问题的用户建议使用(my-clips)项目.我知道了,您可以下载.但是这个项目很复杂,我很困惑.

Some users in response to questions like this suggested that the use of the (my-clips) project. I get this, you can download this. But this project is complex and I am confused.

有人可以给我看一个非常简单的例子吗?谢谢

can anyone show me a very simple example please? thank you

推荐答案

我这样做的方式是:

final ClipboardManager clipboard = (ClipboardManager) this.getSystemService(CLIPBOARD_SERVICE);
clipboard.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
    public void onPrimaryClipChanged() {
        String a = clipboard.getText().toString();
        Toast.makeText(getBaseContext(), "Copy:\n" + a, Toast.LENGTH_LONG).show();
    }
});

以这种方式进行操作,无需提供服务,添加到清单或任何东西,只需先打开您的应用程序,然后将其关闭,然后从任意位置复制文本即可复制并显示在您的应用程序中

do it this way without service, add to manifest or anything, just open your app first then close it, and copy the text from anywhere to copy and show up in your app

这篇关于服务-android剪贴板监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆