티스토리 뷰
권한을 부여하면 다른앱의 상태등을 가져올수 있는 기능을 제공한다.
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
- rxswift
- CentOS
- android
- docker
- centos8
- MySQL
- php
- Linux
- git
- nodejs
- Java
- cocoapods
- enum
- Codable
- Gradle
- ios
- windows10
- ubuntu
- Spring
- go
- SWIFT
- Xcode
- war
- Windows
- tomcat
- golang
- intellij
- github
- Kotlin
- Python
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |