Example usage for android.app.usage UsageStats getLastTimeUsed

List of usage examples for android.app.usage UsageStats getLastTimeUsed

Introduction

In this page you can find the example usage for android.app.usage UsageStats getLastTimeUsed.

Prototype

public long getLastTimeUsed() 

Source Link

Document

Get the last time this package was used, measured in milliseconds since the epoch.

Usage

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

private static String printForegroundTask(Context context) {
    currentApp = "NULL";
    try {/*from ww  w.  ja  v  a2s.c o m*/
        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;
    }//w  w  w.ja v a 2 s .c  o m

    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);//from  w ww  . j  ava 2  s .  co  m
    }

    return entries;
}