티스토리 뷰
A minimalist example of Android accessibility service
Event 종류를 받아서 판별해주는 Service
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);
}
}
$ 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
...
'Computer > Android' 카테고리의 다른 글
[Android] key event value (0) | 2020.11.07 |
---|---|
[Kotlin] Basic Syntax (기본 문법) (0) | 2020.11.03 |
[Android] drawLine, drawPath 사용법 가이드 (0) | 2020.11.03 |
[Android] Android Service에서 AlertDialog 호출 (0) | 2020.10.26 |
[Android] Canvas TextView DP, Pixel 설정 (0) | 2020.10.19 |
댓글