java - Android:获取每小时的使用情况统计信息

标签 java android usage-statistics

我使用Android的UsageStats功能,但最小间隔是DAILY INTERVAL

long time = System.currentTimeMillis();
List<UsageStats> appList = manager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - DAY_IN_MILLI_SECONDS, time);

如何按小时间隔获取 UsageStats

最佳答案

所有功劳归this answer 。我从那件事中学到了教训。

我们如何收集自定义时间范围(例如每 1 小时)的应用使用数据?

我们必须调用queryEvents(long begin_time, long end_time)方法,因为它将为我们提供从 begin_timeend_time 的所有数据。它通过 foregroundbackground 事件为我们提供每个应用程序数据,而不是像 queryUsageStats() 这样的总花费时间。方法。因此,使用前台和后台事件时间戳,我们可以统计应用程序的启动次数,也可以找出每个应用程序的使用时长。

实现收集最近 1 小时的应用使用数据

首先,在AndroidManifest.xml文件中添加以下行,并请求用户获得使用访问权限。

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />

在任何方法中添加以下行

    long hour_in_mil = 1000*60*60; // In Milliseconds
    long end_time = System.currentTimeMillis();
    long start_time = end_time - hour_in_mil;

然后,调用方法getUsageStatistics()

    getUsageStatistics(start_time, end_time);

getUsageStatistics 方法

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
void getUsageStatistics(long start_time, long end_time) {

    UsageEvents.Event currentEvent;
  //  List<UsageEvents.Event> allEvents = new ArrayList<>();
    HashMap<String, AppUsageInfo> map = new HashMap<>();
    HashMap<String, List<UsageEvents.Event>> sameEvents = new HashMap<>();

    UsageStatsManager mUsageStatsManager = (UsageStatsManager)
            context.getSystemService(Context.USAGE_STATS_SERVICE);

    if (mUsageStatsManager != null) {
        // Get all apps data from starting time to end time
        UsageEvents usageEvents = mUsageStatsManager.queryEvents(start_time, end_time);

        // Put these data into the map
        while (usageEvents.hasNextEvent()) {
            currentEvent = new UsageEvents.Event();
            usageEvents.getNextEvent(currentEvent);
            if (currentEvent.getEventType() == UsageEvents.Event.ACTIVITY_RESUMED ||
                    currentEvent.getEventType() == UsageEvents.Event.ACTIVITY_PAUSED) {
              //  allEvents.add(currentEvent);
                String key = currentEvent.getPackageName();
                if (map.get(key) == null) {
                    map.put(key, new AppUsageInfo(key));
                    sameEvents.put(key,new ArrayList<UsageEvents.Event>());
                }
                sameEvents.get(key).add(currentEvent);
            }
        }

        // Traverse through each app data which is grouped together and count launch, calculate duration
        for (Map.Entry<String,List<UsageEvents.Event>> entry : sameEvents.entrySet()) {
            int totalEvents = entry.getValue().size();
            if (totalEvents > 1) {
                for (int i = 0; i < totalEvents - 1; i++) {
                    UsageEvents.Event E0 = entry.getValue().get(i);
                    UsageEvents.Event E1 = entry.getValue().get(i + 1);

                    if (E1.getEventType() == 1 || E0.getEventType() == 1) {
                        map.get(E1.getPackageName()).launchCount++;
                    }

                    if (E0.getEventType() == 1 && E1.getEventType() == 2) {
                        long diff = E1.getTimeStamp() - E0.getTimeStamp();
                        map.get(E0.getPackageName()).timeInForeground += diff;
                    }
                }
            }

    // If First eventtype is ACTIVITY_PAUSED then added the difference of start_time and Event occuring time because the application is already running.
            if (entry.getValue().get(0).getEventType() == 2) {
                long diff = entry.getValue().get(0).getTimeStamp() - start_time;
                map.get(entry.getValue().get(0).getPackageName()).timeInForeground += diff;
            }
            
    // If Last eventtype is ACTIVITY_RESUMED then added the difference of end_time and Event occuring time because the application is still running .
            if (entry.getValue().get(totalEvents - 1).getEventType() == 1) {
                long diff = end_time - entry.getValue().get(totalEvents - 1).getTimeStamp();
                map.get(entry.getValue().get(totalEvents - 1).getPackageName()).timeInForeground += diff;
            }
        }
    
    smallInfoList = new ArrayList<>(map.values());

    // Concatenating data to show in a text view. You may do according to your requirement
    for (AppUsageInfo appUsageInfo : smallInfoList)
    {
        // Do according to your requirement
        strMsg = strMsg.concat(appUsageInfo.packageName + " : " + appUsageInfo.launchCount + "\n\n");
    }

    TextView tvMsg = findViewById(R.id.MA_TvMsg);
    tvMsg.setText(strMsg);
       
    } else {
        Toast.makeText(context, "Sorry...", Toast.LENGTH_SHORT).show();
    }

}

AppUsageInfo.class

import android.graphics.drawable.Drawable;

class AppUsageInfo {
    Drawable appIcon; // You may add get this usage data also, if you wish.
    String appName, packageName;
    long timeInForeground;
    int launchCount;

    AppUsageInfo(String pName) {
        this.packageName=pName;
    }
}

如何自定义这些代码来收集每 1 小时的数据?

由于您想要获取每小时数据,请更改每小时数据的 end_timestart_time 值。例如:如果我尝试收集过去每小时的数据(过去 2 小时的数据)。我会执行以下操作。

    long end_time = System.currentTimeMillis();
    long start_time = end_time - (1000*60*60);

    getUsageStatistics(start_time, end_time);

    end_time =  start_time;
    start_time = start_time - hour_in_mil;

    getUsageStatistics(start_time, end_time);

但是,您可以使用Handler来跳过重复写入start_timeend_time来更改这些变量的值。每收集一小时的数据,就会完成一个任务,自动更改变量的值后,您将再次调用 getUsageStatistics 方法。

注意:也许,您将无法检索过去 7.5 天以上的数据,例如 events are only kept by the system for a few days .

关于java - Android:获取每小时的使用情况统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59113756/

相关文章:

java - 为什么 selenium Web 驱动程序在此网站上找不到文本字段?

android - 如何导航回多个屏幕?

android - 在 RecyclerView 中水平居中项目(使用 GridLayoutManager)

android - 如何在 Vector Drawable 中对图标的特定部分进行动画处理?

Android:获取每次 Activity 花费的时间

android - 如何获取已安装应用的电池使用详情?

java - Activity 开始时倒计时器

java - 如何有选择地跳过 Flux 上的多个处理步骤

java - 在 Web 服务器上代表 Java 中的用户使用 google api

安卓市场应用版本分布