简体   繁体   English

在浏览器中共享所选文本到设备上安装的应用

[英]Share selected text in browser to apps installed on a device

Good day everyboby. 大家好。 I have not very trivial task on project im working on. 我对正在进行的项目不是很琐碎的任务。 Main part of the project is web site. 该项目的主要部分是网站。 The task is to allow user to select text on web site opened in any browser on android device and with some magic allow to share it to another apps installed on device. 任务是允许用户选择在android设备上的任何浏览器中打开的网站上的文本,并通过一些魔术将其共享给设备上安装的其他应用。 Such as Skype, Viber or any other. 例如Skype,Viber或任何其他。 The problem is that there is no any app on device which i own, and should not be. 问题是我拥有的设备上没有任何应用程序,也不应该拥有。 And browser can be any. 浏览器可以是任何浏览器。 Will apreciate any advice. 将赞赏任何建议。 Thanks in advance 提前致谢

You have to monitor clipboard changes. 您必须监视剪贴板更改。 So whenever user copy's text from any source you can listen to it. 因此,只要用户从任何来源复制文本,您都可以收听。

You have to create a service and listen to clipboard changes. 您必须创建一个服务并听剪贴板更改。

Create a service: 创建服务:

public class ClipboardMonitorService extends Service {
    private static final String TAG = "ClipboardManager";
    private static final String FILENAME = "clipboard-history.txt";

    private File mHistoryFile;
    private ExecutorService mThreadPool = Executors.newSingleThreadExecutor();
    private ClipboardManager mClipboardManager;

    @Override
    public void onCreate() {
        super.onCreate();

        // TODO: Show an ongoing notification when this service is running.
        mHistoryFile = new File(getExternalFilesDir(null), FILENAME);
        mClipboardManager =
                (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        mClipboardManager.addPrimaryClipChangedListener(
                mOnPrimaryClipChangedListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mClipboardManager != null) {
            mClipboardManager.removePrimaryClipChangedListener(
                    mOnPrimaryClipChangedListener);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    private ClipboardManager.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener =
            new ClipboardManager.OnPrimaryClipChangedListener() {
        @Override
        public void onPrimaryClipChanged() {
            Log.d(TAG, "onPrimaryClipChanged");
            ClipData clip = mClipboardManager.getPrimaryClip();
            mThreadPool.execute(new WriteHistoryRunnable(
                    clip.getItemAt(0).getText()));
        }
    };

    private class WriteHistoryRunnable implements Runnable {
        private final Date mNow;
        private final CharSequence mTextToWrite;

        public WriteHistoryRunnable(CharSequence text) {
            mNow = new Date(System.currentTimeMillis());
            mTextToWrite = text;
        }

        @Override
        public void run() {
            if (TextUtils.isEmpty(mTextToWrite)) {
                // Don't write empty text to the file
                return;
            }

            if (isExternalStorageWritable()) {
                try {
                    Log.i(TAG, "Writing new clip to history:");
                    Log.i(TAG, mTextToWrite.toString());
                    BufferedWriter writer =
                            new BufferedWriter(new FileWriter(mHistoryFile, true));
                    writer.write(String.format("[%s]: ", mNow.toString()));
                    writer.write(mTextToWrite.toString());
                    writer.newLine();
                    writer.close();
                } catch (IOException e) {
                    Log.w(TAG, String.format("Failed to open file %s for writing!",
                            mHistoryFile.getAbsoluteFile()));
                }
            } else {
                Log.w(TAG, "External storage is not writable!");
            }
        }
    }
}

Add this to your Manifest: 将此添加到清单中:

<service
    android:name=".service.ClipboardMonitorService"
    android:label="Clipboard Monitor"
    android:exported="false"/>

Source: https://github.com/twaddington/Android-Clipboard-Monitor/blob/master/src/com/example/clipboardmonitor/service/ClipboardMonitorService.java 来源: https : //github.com/twaddington/Android-Clipboard-Monitor/blob/master/src/com/example/clipboardmonitor/service/ClipboardMonitorService.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在列表中获取所有共享应用程序(安装在设备上)? - How to get all share apps (installed on the device) in a list? 从浏览器的共享意图访问选定的文本和URL - Access selected text and URL from browser's share intent SHARE INTENT Intent.createChooser:如何显示所有已安装的文件浏览器应用程序? - SHARE INTENT Intent.createChooser: How to display all installed file browser apps? 通过android中的facebook / twitter应用程序共享,如果设备上安装了facebook / twitter,请不要打开浏览器 - Share via facebook/twitter application in android , don't open browser if facebook/twitter is installed on the device 离子在带有图标的安卓设备上安装应用程序 - Ionic get installed apps on android device with icon Android CTS是否会测试设备中已安装的应用程序? - Does Android CTS test installed apps in device? 如何确定在Android设备上安装的应用程序? - How to determine apps installed on Android device? 如何获取 Android 设备上已安装应用程序的列表 - How to get a list of installed apps on Android device 获取Android设备上已安装的应用程序数量? - Get number of installed apps on an Android device? 离子在Android设备上安装应用程序 - Ionic get installed apps on android device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM