当前位置:网站首页>Get a list of recent apps

Get a list of recent apps

2022-04-23 18:41:00 tanleicsdn

To get the list of recently used apps UsageStatsManager class , You also need to apply for permission to allow the use of defense applications .

private void getPackagesInfo() {
        UsageStatsManager manager = (UsageStatsManager) getApplicationContext().getSystemService(USAGE_STATS_SERVICE);
        List<UsageStats> stats = manager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, PhoneUtils.getTimesMorning().getTime(), PhoneUtils.getTimesNight().getTime());
        PackageManager pm = getPackageManager();
        for (UsageStats us : stats) {
            ApplicationInfo applicationInfo = null;
            try {
                applicationInfo = pm.getApplicationInfo(us.getPackageName(), PackageManager.GET_META_DATA);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            if (applicationInfo != null) {
                if ((applicationInfo.flags & applicationInfo.FLAG_SYSTEM) <= 0) {
                   	" Application duration " = (int) (us.getTotalTimeInForeground() / 1000);
                    " apply name " = applicationInfo.loadLabel(getPackageManager()).toString();
                }
            }
        }
    }
public class PhoneUtils {
    // Get the day 0 Point time 
    public static Date getTimesMorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTime());
    }

    // Get the day 24 Point time 
    public static Date getTimesNight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // Get the day 24 Time stamp 
    public static long getTimesNow() {
        return System.currentTimeMillis();
    }
}

The above completes the function of obtaining the duration of recently used applications .

版权声明
本文为[tanleicsdn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210604297388.html