Example usage for android.app.usage UsageStatsManager INTERVAL_DAILY

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

Introduction

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

Prototype

int INTERVAL_DAILY

To view the source code for android.app.usage UsageStatsManager INTERVAL_DAILY.

Click Source Link

Document

An interval type that spans a day.

Usage

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

private static String printForegroundTask(Context context) {
    currentApp = "NULL";
    try {/*from w w w. jav a2  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  ww  w  .j  a  v a2 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;
}