android - How can I determine if the back key has been pressed in a AccessibilityService, onKeyDown doesn't respond? -


i'd able know in accessibilityservice when user presses system key, key. i've overwritten onkeyevent method, doesn't respond @ all, yet events onaccessibilityevent.

@override public boolean onkeyevent(keyevent event) {     log.e(tag, "------------------- onkeyevent ----------------------------");     return super.onkeyevent(event); } 

following configuration files, i'm monitoring accessibilityservice package (a subproject) , actual application package.

manifest.xml subproject containing accessibilityservice.

<?xml version="1.0" encoding="utf-8"?> 

...

<service android:name="com.foo.myaccessibilityservice"     android:permission="android.permission.bind_accessibility_service">   <intent-filter>     <action android:name="android.accessibilityservice.accessibilityservice" />   </intent-filter>   <meta-data android:name="android.accessibilityservice"       android:resource="@xml/accessibilityservice" /> </service> 

accessibilityservice.xml file follows:

<?xml version="1.0" encoding="utf-8"?>  <accessibility-service xmlns:tools="http://schemas.android.com/tools" android:accessibilityeventtypes="typeallmask" android:accessibilityfeedbacktype="feedbackgeneric" android:accessibilityflags="flagincludenotimportantviews" xmlns:android="http://schemas.android.com/apk/res/android" android:description="@string/emachine_accessibility_service_description" android:settingsactivity="com.foo.services.myaccessibilityservice" tools:ignore="unusedattribute" android:canretrievewindowcontent="true" android:canrequestfilterkeyevents="true" android:canrequesttouchexplorationmode="true"/> 

and yes com.foo.services.myaccessibilityservice 'on' under system accessibility settings.

added

    android:canrequestfilterkeyevents="true" android:canrequesttouchexplorationmode="true" 

to accessibility.xml in above, , equivalent in onserviceconnected added programmatically.

        if (build.version.sdk_int >= build.version_codes.jelly_bean_mr2) {         info.flags |= accessibilityserviceinfo.flag_report_view_ids;         info.flags |= accessibilityserviceinfo.capability_can_request_filter_key_events;         info.flags |= accessibilityserviceinfo.capability_can_retrieve_window_content;     } 

with no positive outcome

i suspect trying intercept these events soft keyboard not sending them. key events reliably sent hardware keyboards. in fact, manually sending events software input method service discouraged, , in prior versions of os lead potentially malicious effects. android keyevent documentation:

as soft input methods can use multiple , inventive ways of inputting text, there no guarantee key press on soft keyboard generate key event: left ime's discretion, , in fact sending such events discouraged. should never rely on receiving keyevents key on soft input method. in particular, default software keyboard never send key event application targetting jelly bean or later, , send events presses of delete , return keys applications targetting ice cream sandwich or earlier. aware other software input methods may never send key events regardless of version. consider using editor actions ime_action_done if need specific interaction software keyboard, gives more visibility user how application react key presses.

a cheap bluetooth keyboard best bet. or emulators proper configuration.

edit:

alternatively may have error in configuration file. in accessibilityservice.xml, replace line:

android:accessibilityflags="flagincludenotimportantviews" 

with this:

android:accessibilityflags="flagincludenotimportantviews|flagrequestfilterkeyevents" 

you can acheive same in accessibility service code.

protected void onserviceconnected() {     super.onserviceconnected();      accessibilityserviceinfo tempinfo = getserviceinfo();     tempinfo.flags |= accessibilityserviceinfo.flag_include_not_important_views;     tempinfo.flags |= accessibilityserviceinfo.flag_request_filter_key_events;     setserviceinfo(tempinfo);  } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -