UsageStatsManager
can be used to retrieve the usage history of applications, including information about the time and frequency of app usage on the device through the queryUsageStats
method. The most recently used application is highly likely to be in the foreground state. Periodically, call the queryUsageStats
method to update the data.
- Easy to implement.
- Does not require access to personal information.
- Data is updated with a delay (not in real time).
- Using
UsageStatsManager
requires declaring thePACKAGE_USAGE_STATS
permission. This permission does not require manual user consent but must be enabled in Settings > Apps > Special app access > Usage access. - The application must be transparent with users about how this data is used and comply with Google Play’s privacy policies.
Click to expand
-
Declare permissions in
AndroidManifest.xml
:<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />
-
Check permission in the code:
private boolean isUsageAccessGranted() { AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), getPackageName()); return mode == AppOpsManager.MODE_ALLOWED; }
-
Query information using
UsageStatsManager
:UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); if (usageStatsManager == null) { return new ArrayList<>(); // Return empty list if UsageStatsManager is unavailable } // Query usage stats for the last 24 hours List<UsageStats> usageStatsList = usageStatsManager.queryUsageStats( UsageStatsManager.INTERVAL_DAILY, currentTime - (24 * 60 * 60 * 1000), // Start time (24 hours ago) currentTime // End time (now) ); if (usageStatsList == null || usageStatsList.isEmpty()) { return new ArrayList<>(); // Return empty list if no data is available }
AccessibilityService
has the ability to monitor apps in the foreground, allowing you to retrieve app information via the TYPE_WINDOW_STATE_CHANGED
event.
- Data is updated in real-time.
- Complex to implement.
- Granted permissions can be revoked when the application is closed.
- When granting
AccessibilityService
access, other information like text content, gesture behavior, etc., may also be accessed.
- Using
AccessibilityService
requires the app to clearly explain the reason for requesting this permission and not misuse it to collect unrelated data. - According to Google Play policy, this permission is only accepted if it serves a purpose that aids the user, such as supporting disabled users.
- Misuse of this permission could result in the app being removed from Google Play.
Click to expand
-
Create a class that extends
AccessibilityService
:public class AccessibilityServiceExtend extends AccessibilityService { @Override public void onAccessibilityEvent(AccessibilityEvent event) { if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { // Get the package name and activity name ComponentName componentName = new ComponentName( event.getPackageName().toString(), event.getClassName().toString() ); String currentPackageName = componentName.getPackageName(); String currentActivityName = componentName.flattenToShortString(); } } @Override public void onInterrupt() { } }
-
Declare the service in
AndroidManifest.xml
:<!--android:foregroundServiceType="mediaPlayback" is used for the post notification feature--> <service android:name=".AccessibilityServiceExtend" android:exported="true" android:foregroundServiceType="mediaPlayback" 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>
-
Configure the file
res/xml/accessibility_config.xml
:<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeWindowStateChanged" android:accessibilityFeedbackType="feedbackGeneric" android:canRetrieveWindowContent="false" android:description="@string/app_name" android:notificationTimeout="100" />
-
Activate the service in the device’s Accessibility settings:
- The user needs to enable the service in Settings > Accessibility > ForegroundActivity.
Feature | UsageStatsManager | AccessibilityService |
---|---|---|
Setup | Easy | Complex |
Update Frequency | Delayed | Real-time |
Permission Scope | Does not require sensitive access | Requires high and sensitive permissions |
Accuracy | Depends on query timing | High accuracy via system events |
Policy Compliance | Requires transparent declaration | Strict requirements to avoid misuse |
Note: The choice of method depends on project requirements and the priority among performance, security, and compliance with Google’s policies.