Created
February 7, 2018 08:14
-
-
Save dobrowins/0c027570f91957d25f2fe62933fe35c1 to your computer and use it in GitHub Desktop.
Detecting foreground activity from service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- These options MUST be specified here in order for the events to be received on first | |
start in Android 4.1.1 --> | |
<accessibility-service | |
xmlns:tools="http://schemas.android.com/tools" | |
android:accessibilityEventTypes="typeWindowStateChanged" | |
android:accessibilityFeedbackType="feedbackGeneric" | |
android:accessibilityFlags="flagIncludeNotImportantViews" | |
android:description="@string/app_name" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
tools:ignore="UnusedAttribute"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<application> | |
<service | |
android:label="BLABLABLA" | |
android:name=".service.WindowChangeDetectingService" | |
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> | |
</application> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WindowChangeDetectingService : AccessibilityService() { | |
override fun onServiceConnected() { | |
super.onServiceConnected() | |
//Configure these here for compatibility with API 13 and below. | |
val config = AccessibilityServiceInfo() | |
config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED | |
config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC | |
if (Build.VERSION.SDK_INT >= 16) | |
//Just in case this helps | |
config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS | |
serviceInfo = config | |
} | |
override fun onAccessibilityEvent(event: AccessibilityEvent) { | |
if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { | |
if (event.packageName != null && event.className != null) { | |
val componentName = ComponentName( | |
event.packageName.toString(), | |
event.className.toString() | |
) | |
val activityInfo = tryGetActivity(componentName) | |
val isActivity = activityInfo != null | |
if (isActivity) | |
Timber.d("CurrentActivity: %s", componentName.flattenToShortString()) | |
} | |
} | |
} | |
private fun tryGetActivity(componentName: ComponentName): ActivityInfo? { | |
try { | |
return packageManager.getActivityInfo(componentName, 0) | |
} catch (e: PackageManager.NameNotFoundException) { | |
return null | |
} | |
} | |
override fun onInterrupt() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment