• ReflectUtil


    package com.android.demo.lileidemo.utils;

    import android.app.usage.UsageStats;
    import android.util.Log;

    import com.android.demo.lileidemo.MyApplication;
    import com.android.demo.lileidemo.constant.AppConstants;

    import java.lang.reflect.Field;
    import java.util.List;

    /**
    * date: 04/16/2020.
    * author: lilei.
    */
    public class ReflectUtil {
    private static final String TAG = AppConstants.APP_TAG + "ReflectUtil ";

    private static UsageStats mUsageStats;
    private static String mPackageName;

    public static synchronized void testReflect() {
    long currentTimeMillis = System.currentTimeMillis();
    long hourStartMillis = DateTimeUtil.getHourStartMillis(currentTimeMillis);
    long startTimeMillis = 1586941200000L;//hourStartMillis - AppConstants.OPEN_APP_STATS_INTERVAL_MILLIS;
    long endTimeMillis = 1586941464278L;
    List<UsageStats> listUsageStats = UsageStatsUtil.getUsageStatsList(
    MyApplication.getAppContext(), startTimeMillis, endTimeMillis);

    for (int i = 0; i < listUsageStats.size(); i++) {
    mUsageStats = listUsageStats.get(i);
    getAppLaunchCount(mUsageStats);
    mPackageName = mUsageStats.getPackageName();
    long totalTimeInForeground = mUsageStats.getTotalTimeInForeground();
    Log.d(TAG, "collectAllOpenApp() mPackageName:" + mPackageName
    + " totalTimeInForeground:" + totalTimeInForeground
    + " getFirstTimeStamp:" + mUsageStats.getFirstTimeStamp()
    + " getLastTimeStamp:" + mUsageStats.getLastTimeStamp()
    + " getLastTimeUsed:" + mUsageStats.getLastTimeUsed()
    + " startTimeMillis:" + startTimeMillis + " endTimeMillis:" + endTimeMillis);
    }
    }

    private static void getAppLaunchCount(UsageStats usageStats) {
    Class<UsageStats> usageStatsClass = UsageStats.class;
    try {
    //step1 通过方法名称反射
    // Method method = usageStatsClass.getDeclaredMethod("getLastTimeUsed",
    // (Class[]) null);
    // //Open permission.
    // method.setAccessible(true);
    // Object lastTimeUsed = (Object) method.invoke(usageStats, (Object[]) null);
    //


    //step2 通过变量名称反射
    Class c = usageStats.getClass();
    Field field = c.getDeclaredField("mLaunchCount");
    //Field field = usageStats.getClass().getDeclaredField("mAppLaunchCount");
    field.setAccessible(true);
    int launchCount = field.getInt(usageStats);

    field = c.getDeclaredField("mLastEvent");
    //Field field = usageStats.getClass().getDeclaredField("mAppLaunchCount");
    field.setAccessible(true);
    int lastEvent = field.getInt(usageStats);

    Log.d(TAG, "getAppLaunchCount() *# getPackageName:" + usageStats.getPackageName()
    + " lastEvent:" + lastEvent + " launchCount:" + launchCount);
    } catch (Exception e) {
    Log.d(TAG, "getAppLaunchCount() error:" + e.toString());
    e.printStackTrace();
    }
    }
    }
  • 相关阅读:
    vs编译在win xp电脑上运行的win32程序遇到的问题记录(无法定位程序输入点GetTickCount64于动态链接库KERNEL32.dll)
    (转载)用VS2012或VS2013在win7下编写的程序在XP下运行就出现“不是有效的win32应用程序“
    记录编译方面的问题(重定义)
    记录一个问题:win32程序release版本和debug版本运行效果不同
    C++复制、压缩文件夹
    foreach 和 list.foreach 初步测试
    转载字典地址:http://blog.csdn.net/aladdinty/article/details/3591789
    WindowsApi 解压缩文件
    23种简洁好看的扁平化模板
    Session为null 问题
  • 原文地址:https://www.cnblogs.com/adamli/p/13140749.html
Copyright © 2020-2023  润新知