Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a minimalist example of Android accessibility service
$ adb logcat -s RecorderService
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
V/RecorderService( 1841): onServiceConnected
...
V/RecorderService( 1841): onAccessibilityEvent: [type] TYPE_VIEW_CLICKED [class] android.widget.Button [package] com.example [time] 7710428 [text] Activity1
V/RecorderService( 1841): onAccessibilityEvent: [type] TYPE_VIEW_FOCUSED [class] android.widget.EditText [package] com.example [time] 7710521 [text]
V/RecorderService( 1841): onAccessibilityEvent: [type] TYPE_WINDOW_STATE_CHANGED [class] com.example.Activity1 [package] com.example [time] 7710536 [text] TestRecord
V/RecorderService( 1841): onAccessibilityEvent: [type] TYPE_VIEW_FOCUSED [class] android.widget.EditText [package] com.example [time] 7710539 [text]
V/RecorderService( 1841): onAccessibilityEvent: [type] TYPE_VIEW_CLICKED [class] android.widget.EditText [package] com.example [time] 7725471 [text]
V/RecorderService( 1841): onAccessibilityEvent: [type] TYPE_VIEW_TEXT_CHANGED [class] android.widget.EditText [package] com.example [time] 7728589 [text] f
V/RecorderService( 1841): onAccessibilityEvent: [type] TYPE_VIEW_TEXT_CHANGED [class] android.widget.EditText [package] com.example [time] 7728804 [text] fg
V/RecorderService( 1841): onAccessibilityEvent: [type] TYPE_VIEW_TEXT_CHANGED [class] android.widget.EditText [package] com.example [time] 7728994 [text] fgj
...
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
public class RecorderService extends AccessibilityService {
static final String TAG = "RecorderService";
private String getEventType(AccessibilityEvent event) {
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
return "TYPE_NOTIFICATION_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_CLICKED:
return "TYPE_VIEW_CLICKED";
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
return "TYPE_VIEW_FOCUSED";
case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
return "TYPE_VIEW_LONG_CLICKED";
case AccessibilityEvent.TYPE_VIEW_SELECTED:
return "TYPE_VIEW_SELECTED";
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
return "TYPE_WINDOW_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
return "TYPE_VIEW_TEXT_CHANGED";
}
return "default";
}
private String getEventText(AccessibilityEvent event) {
StringBuilder sb = new StringBuilder();
for (CharSequence s : event.getText()) {
sb.append(s);
}
return sb.toString();
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.v(TAG, String.format(
"onAccessibilityEvent: [type] %s [class] %s [package] %s [time] %s [text] %s",
getEventType(event), event.getClassName(), event.getPackageName(),
event.getEventTime(), getEventText(event)));
}
@Override
public void onInterrupt() {
Log.v(TAG, "onInterrupt");
}
@Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.v(TAG, "onServiceConnected");
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.flags = AccessibilityServiceInfo.DEFAULT;
info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
setServiceInfo(info);
}
}
@stevehb
Copy link

stevehb commented Jul 14, 2015

For API >= 14, it's probably easier to get the event type string by using AccessibilityEvent. eventTypeToString(int).

@raluca-elena
Copy link

raluca-elena commented Oct 9, 2015

thanks for the example. saved my time!

@omkarchavan25
Copy link

omkarchavan25 commented Jun 8, 2016

through which event will i get the contact number of clicked contact. can i get the code for it.

@BruceAnda
Copy link

BruceAnda commented Jun 13, 2016

thank you! this example is help me.

@srujanb
Copy link

srujanb commented Jul 17, 2016

I was searching for something like this. Thank you so much.

@sotraBel
Copy link

sotraBel commented Jul 18, 2016

Can you explan me Plz ?
Is the use of the accessibility services requires that I must have a special version of Android,
I currently have the 4.1.2, but no service works fo me;
When iIverifie my phone , i don't find the specific ICON shown above : Home...Setting......

@ibrahimsn98
Copy link

ibrahimsn98 commented Sep 21, 2016

Thank you :))

@ehsanheidari67
Copy link

ehsanheidari67 commented Jan 8, 2017

Thank you! Exactly what I wanted

@Swethamr402
Copy link

Swethamr402 commented May 4, 2017

Thank you!!! It helped me a lot. I need to paste response on edittext. can you help me on it?

@alwaysblack
Copy link

alwaysblack commented May 10, 2017

What does the value returned by getEventTime() represent?

@dinhpv38
Copy link

dinhpv38 commented Aug 23, 2017

tks you very much:D

@dedsec1911
Copy link

dedsec1911 commented Aug 18, 2018

I have a server based realtime keylogger, the issue is when I type in passwords it sends null value to the server ! What can I do to fix this ?
The code is written on android and keystrokes are monitored by Accessibility Event handler.

@AndroidDeveloperLB
Copy link

AndroidDeveloperLB commented Sep 29, 2019

In which cases do "onInterrupt" get called? Isn't it the same as "onDestroy" ? I thought that the OS will try to keep it alive as long as possible, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment