Example usage for android.app.usage UsageStats getPackageName

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

Introduction

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

Prototype

public String getPackageName() 

Source Link

Usage

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.j av  a  2s .com

    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 .java 2  s . c o  m
    }

    return entries;
}