Example usage for android.app.usage UsageStatsManager queryUsageStats

List of usage examples for android.app.usage UsageStatsManager queryUsageStats

Introduction

In this page you can find the example usage for android.app.usage UsageStatsManager queryUsageStats.

Prototype

public List<UsageStats> queryUsageStats(int intervalType, long beginTime, long endTime) 

Source Link

Document

Gets application usage stats for the given time range, aggregated by the specified interval.

Usage

From source file:nu.yona.app.api.service.ActivityMonitorService.java

private static String printForegroundTask(Context context) {
    currentApp = "NULL";
    try {/*from w w  w . j ava  2 s.  com*/
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
            long time = System.currentTimeMillis();
            List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
                    time - AppConstant.ONE_SECOND * AppConstant.ONE_SECOND, time);
            if (appList != null && appList.size() > 0) {
                SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
                for (UsageStats usageStats : appList) {
                    mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
                }
                if (!mySortedMap.isEmpty()) {
                    currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
                }
            }
        } else {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            currentApp = am.getRunningAppProcesses().get(0).processName;
        }
    } catch (Exception e) {
        AppUtils.reportException(ActivityMonitorService.class.getSimpleName(), e, Thread.currentThread());
    }
    return currentApp;
}

From source file:com.tasomaniac.openwith.resolver.ResolverActivity.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private String getCallerPackageLollipop() {
    UsageStatsManager mUsm = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
    long time = System.currentTimeMillis();
    // We get usage stats for the last 10 seconds
    List<UsageStats> stats = mUsm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
            time - 10 * DateUtils.SECOND_IN_MILLIS, time);
    if (stats == null) {
        return null;
    }/*from w  w  w. j a  v a  2s .c om*/

    UsageStats lastUsage = null;
    for (UsageStats currentUsage : stats) {
        String currentPackage = currentUsage.getPackageName();
        if (BuildConfig.APPLICATION_ID.equals(currentPackage) || "android".equals(currentPackage)) {
            continue;
        }
        if (lastUsage == null || lastUsage.getLastTimeUsed() < currentUsage.getLastTimeUsed()) {
            lastUsage = currentUsage;
        }
    }
    if (lastUsage != null) {
        return lastUsage.getPackageName();
    }

    return null;
}

From source file:com.farmerbb.taskbar.service.TaskbarService.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private List<AppEntry> getAppEntriesUsingUsageStats() {
    UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
    List<UsageStats> usageStatsList = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY,
            searchInterval, System.currentTimeMillis());
    List<AppEntry> entries = new ArrayList<>();

    for (UsageStats usageStats : usageStatsList) {
        AppEntry newEntry = new AppEntry(usageStats.getPackageName(), null, null, null, false);

        newEntry.setTotalTimeInForeground(usageStats.getTotalTimeInForeground());
        newEntry.setLastTimeUsed(usageStats.getLastTimeUsed());
        entries.add(newEntry);/* www  .  j  av  a2s  .  c o m*/
    }

    return entries;
}

From source file:research.sg.edu.edapp.kb.KbSoftKeyboard.java

public String getTopPackage() {

    RecentUseComparator mRecentComp = new RecentUseComparator();

    long ts = System.currentTimeMillis();
    UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
    List<UsageStats> usageStats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST,
            ts - 1000 * 10, ts);/*from  ww  w  . ja  v a 2  s  .  com*/
    if (usageStats == null || usageStats.size() == 0) {
        //return NONE_PKG;
        return old_pkg;
    }
    Collections.sort(usageStats, mRecentComp);
    old_pkg = usageStats.get(0).getPackageName();
    return usageStats.get(0).getPackageName();
}