티스토리 뷰

Mobile/Android

[Android] AccessibilityService

out of coding 2016. 8. 2. 16:13

권한을 부여하면 다른앱의 상태등을 가져올수 있는 기능을 제공한다.

AccessibilityService이다.


예로 든것은 다른 앱의 Push가 수신되었을 경우에 대해서임.


Activity.

// 서비스 가동중인지 체크 소스

public static boolean isContainedInAccessbility(Context context) {

AccessibilityManager accessibilityManager = (AccessibilityManager)context.getSystemService(Context.ACCESSIBILITY_SERVICE);

List<AccessibilityServiceInfo> serviceList = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);

return serviceList.toString().contains(context.getPackageName());

}

// 서비스 가동중인지 체크해서 Setting 화면 띄우기

if(!isContainedInAccessbility(getBaseContext())) {

// Alert으로 대략 내가 왜 이걸 한다라는걸 보여주면 좋음.

Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);

startActivityForResult(intent, 111);

}


Service

public class MyAssessibilityService extends AccessibilityService {


@Override

public void onAccessibilityEvent(AccessibilityEvent event) {

String msg;


if(TextUtils.isEmpty(event.getText().toString().replace("[", "").replace("]", "").trim())) {

msg = getText(event);

} else {

msg = event.getText().toString();

}


Log.d("TEST", "message = " + msg);

}


@Override

public void onInterrupt() {


}


@Override

protected void onServiceConnected() {

super.onServiceConnected();

Log.d("mrgamza", "service connect");

AccessibilityServiceInfo info = new AccessibilityServiceInfo();

info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;

info.notificationTimeout = 0;

info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;

setServiceInfo(info);

}


private String getText(AccessibilityEvent event) {

try {

Notification notification = (Notification)event.getParcelableData();

RemoteViews views = notification.contentView;

Class<?> secretClass = views.getClass();


Field outerFields[] = secretClass.getDeclaredFields();

for(int i = 0; i < outerFields.length; i++) {

if(!outerFields[i].getName().equals("mActions"))

continue;


outerFields[i].setAccessible(true);


int count = 0;


@SuppressWarnings("unchecked")

ArrayList<Object> actions = (ArrayList<Object>)outerFields[i].get(views);

for(Object action : actions) {

Field innerFields[] = action.getClass().getDeclaredFields();


Object value = null;

Integer type = null;


for(Field field : innerFields) {

field.setAccessible(true);


if(field.getName().equals("value"))

value = field.get(action);

else if(field.getName().equals("type"))

type = field.getInt(action);

}


if(type != null && (type == 9 || type == 10)) {

count++;


if(count == 2)

return value.toString();

}

}

}

} catch(Exception e) {

}


return null;

}

}


string.xml

<string name="accessiblilty_service_description">그냥 여기에 설명 넣읍시다.</string>


manifest

<service

android:name=".MyAssessibilityService"

android:enabled="true"

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/accessibility_service_config"/>

</service>

xml/accessibility_service_config.xml

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

<accessibility-service

xmlns:android="http://schemas.android.com/apk/res/android"

android:accessibilityEventTypes="typeAllMask"

android:accessibilityFeedbackType="feedbackSpoken|feedbackHaptic|feedbackAudible|feedbackGeneric"

android:accessibilityFlags="flagDefault"

android:canRetrieveWindowContent="true"

android:description="@string/accessiblilty_service_description"

android:notificationTimeout="0"

android:settingsActivity="com.example.accesstest.MainActivity"/>



'Mobile > Android' 카테고리의 다른 글

[Android] ScaleType  (0) 2016.08.02
[Android] 설정 화면 띄우기  (0) 2016.08.02
[Android] SMS,MMS,Call Check  (0) 2016.08.01
[Android] Focus  (0) 2016.08.01
[Android] Account 정보 가져오기  (0) 2016.07.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함