Example usage for android.content Context USAGE_STATS_SERVICE

List of usage examples for android.content Context USAGE_STATS_SERVICE

Introduction

In this page you can find the example usage for android.content Context USAGE_STATS_SERVICE.

Prototype

String USAGE_STATS_SERVICE

To view the source code for android.content Context USAGE_STATS_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.usage.UsageStatsManager for querying device usage stats.

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 om
        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.android.tests.usagestats.UsageLogActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
    mLastTime = System.currentTimeMillis() - USAGE_STATS_PERIOD;

    mAdapter = new Adapter();
    setListAdapter(mAdapter);/*from   w ww .  j a v  a2s. co m*/
}

From source file:com.bms.mqp.behaviormodelsystem.AppUsageEventsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mUsageStatsManager = (UsageStatsManager) getActivity().getSystemService(Context.USAGE_STATS_SERVICE); //Context.USAGE_STATS_SERVICE

    mLastTime = System.currentTimeMillis() - USAGE_STATS_PERIOD;

}

From source file:com.bms.mqp.behaviormodelsystem.AppUsageStatisticsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mUsageStatsManager = (UsageStatsManager) getActivity().getSystemService(Context.USAGE_STATS_SERVICE); //Context.USAGE_STATS_SERVICE
}

From source file:com.rui.ruitime.AppUsageStatisticsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mUsageStatsManager = (UsageStatsManager) getActivity().getSystemService(Context.USAGE_STATS_SERVICE); //Context.USAGE_STATS_SERVICE
    setHasOptionsMenu(true);//from w ww.j a  va  2s.c o  m

}

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);/* w ww  . j  ava 2  s .c  o  m*/
    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();
}