Created
August 4, 2015 20:12
-
-
Save lucas-tulio/8b72433fbc7efba704e8 to your computer and use it in GitHub Desktop.
Example of how to use UsageStats in Android
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
/** | |
* AndroidManifest.xml | |
* | |
* <uses-permission | |
* android:name="android.permission.PACKAGE_USAGE_STATS" | |
* tools:ignore="ProtectedPermissions" /> | |
*/ | |
/** | |
* In your Class | |
*/ | |
// Ask user for permission | |
Intent intent = new Intent(android.provider.Settings.ACTION_USAGE_ACCESS_SETTINGS); | |
startActivity(intent); | |
// Show stats | |
long startTime = new GregorianCalendar(2014, 0, 1).getTimeInMillis(); | |
long endTime = new GregorianCalendar(2016, 0, 1).getTimeInMillis(); | |
UsageStatsManager usageStatsManager = (UsageStatsManager)context.getSystemService(Context.USAGE_STATS_SERVICE); | |
List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime); | |
for (UsageStats us : queryUsageStats) { | |
Log.d(TAG, us.getPackageName() + " = " + us.getTotalTimeInForeground()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment