使用AccessibilityService,是否可以找到用户滑动/单击或长按的文本部分?

时间:2018-06-23 14:21:03

标签: android accessibility native accessibilityservice

在Android中,我想查找用户单击或滑动过的文本。我正在尝试使用AccessibilityService。到目前为止,我已经使用AccessibilityNodeInfo获得了屏幕上的所有文本。

但是我无法确定用户单击或滑动的文字。我想阅读用户在屏幕上滑动(或手指移动)的任何文本内容。

到目前为止,如果有帮助,这是我的AccessibilityService。

class MyAccessibilityService :AccessibilityService(){
    companion object {
        @SuppressLint("StaticFieldLeak")
        var  sSharedInstance:MyAccessibilityService? = null
            private set
    }
    override fun onServiceConnected() {
        sSharedInstance = this;
        Log.e("Service started", "yoyo")
    }

    override fun onUnbind(intent: Intent): Boolean {
        sSharedInstance = null;
        return true;
    }
    override fun onInterrupt() {

    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if(event != null && event.source != null/*&& (event.eventType == AccessibilityEvent.TYPE_VIEW_LONG_CLICKED ||
                        event.eventType == AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY)*/) {

            Log.e("Accessibility", "time:" + event?.eventTime + " action: " + event?.action
                    + " type:" + event.eventType)


            displayAllNodes(event.source)
            Log.e("movement", "granular: "+ event.movementGranularity + " from ind: " + event.fromIndex + " toInd: "
                    + event.toIndex + " text: " + event.text + " beforeText: " + event.beforeText)
        } else if(event != null){
            Log.e("event:null", "type: " + event.eventType + " ,")
        }

    }

    private fun displayAllNodes(source: AccessibilityNodeInfo){
        val childCount = source.childCount

        if(childCount <= 0) return
        Log.e("Display_source", source.className.toString() )
        source.extras.keySet().forEach(Consumer {
            Log.e("bundle " + it, source.extras.get(it).toString())
        })
        for(i in 0..childCount-1){
            val child = source.getChild(i)
            Log.e("child At:"+ i, child?.text?.toString()?:"na")

                Log.e("Display_child_source", child?.className.toString()?:"NA" )


            if(child != null)
                displayAllNodes(child)

        }

//        if(windows.isNotEmpty()){
//            val type = windows.get(0)::class.java.simpleName
//        }
    }

   /* override fun onKeyEvent(event: KeyEvent?): Boolean {
        Log.e("key event", "action:" + event?.action + " chars: "+ event?.characters+ " long_press? " + event?.isLongPress)
        if(event?.isLongPress?:false){
            event?.startTracking()
        }
        return super.onKeyEvent(event)
    }*/

    override fun onGesture(gestureId: Int): Boolean {
        Log.e("on_gesture", "id: " + gestureId)
        return super.onGesture(gestureId)
    }
}

0 个答案:

没有答案