Get usage from UsageStatsManager - Android android.app.usage

Android examples for android.app.usage:UsageStatsManager

Description

Get usage from UsageStatsManager

Demo Code


import android.app.ActivityManager;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import java.lang.reflect.Field;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class Main{

    public static boolean isNoSwitch(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            long ts = System.currentTimeMillis();
            UsageStatsManager usageStatsManager = (UsageStatsManager) context
                    .getApplicationContext().getSystemService(
                            Context.USAGE_STATS_SERVICE);
            List<UsageStats> queryUsageStats = usageStatsManager
                    .queryUsageStats(UsageStatsManager.INTERVAL_BEST, 0, ts);
            return !(queryUsageStats == null || queryUsageStats.isEmpty());

        } else {//from  w ww.j a  v  a 2s  . com
            return true;
        }
    }

}

Related Tutorials