从Android

时间:2018-06-12 21:12:47

标签: android kotlin clipboardmanager

我有一个已经正常工作的剪贴板服务。 我只想激活我在特定应用程序上的监听器,即:仅触发我的应用程序上的ClipboardManager.OnPrimaryClipChangedListener

知道我该怎么办?

以下是我服务的一部分:

class ClipboardService: Service() {

    val TAG = "ClipboardManager"
    private val fileName = "clipboard-history.txt"

    private var mHistoryFile: File? = null
    private var mClipboardManager: ClipboardManager? = null
    private val handle = Handler()

    override fun onCreate() {
        super.onCreate()

        mHistoryFile = File(getExternalFilesDir(null), fileName)
        mClipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        mClipboardManager!!.addPrimaryClipChangedListener(mOnPrimaryClipChangedListener)

    private val mOnPrimaryClipChangedListener: ClipboardManager.OnPrimaryClipChangedListener = ClipboardManager.OnPrimaryClipChangedListener {
        Log.d(TAG, "onPrimaryClipChanged")
        val clip: ClipData = mClipboardManager!!.primaryClip
        handle.post({ WriteHistoryRunnable().WriteHistoryRunnable(clip.getItemAt(0).text)})
    }

0 个答案:

没有答案