/** * Returns the current usage stats list, if the user enable the option in the settings menu. * * @param context Context, the current context * @return List<UsageStats> */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static List<UsageStats> getUsageStatsList(Context context) { UsageStatsManager usm = getUsageStatsManager(context); Calendar calendar = Calendar.getInstance(); long endTime = calendar.getTimeInMillis(); calendar.add(Calendar.YEAR, -1); long startTime = calendar.getTimeInMillis(); List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime); return usageStatsList; }
/** * @param context context * @return アプリケーションの使用状況のアクセス許可設定がONの場合true */ public static boolean isUsageStatsAccessible(Context context) { UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.set( calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0); List<UsageStats> usageStatsList = usageStatsManager.queryUsageStats( UsageStatsManager.INTERVAL_DAILY, calendar.getTimeInMillis(), calendar.getTimeInMillis() + 24 * 60 * 60 * 1000); return usageStatsList.size() != 0; }