4

我正在开发一个 Android TV 应用程序(系统应用程序),我正在尝试使用UsageStatsManager来获取最近使用的应用程序列表。
通过以下方法,

UsageStatsManager usm = getUsageStatsManager(context);
Calendar calendar = Calendar.getInstance();
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.YEAR, -1)
final List<UsageStats> usageStatsList = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY, startTime, endTime);

上述方法提供了正确的使用数据,但在设备重新启动后,它不会提供以前使用的应用程序使用数据,直到重新启动后再次打开该应用程序。

但是相同的代码在 android Phone 中可以正常工作(重启后)。

我还在 Android TV 中尝试了来自 Github 的这个示例应用程序,它在 Android TV 中重新启动后也无法提供详细信息(但适用于 Mobile ,两个操作系统版本 8)。

为什么它无法获取 Android TV 的应用程序使用数据?

TIA

4

1 回答 1

1

@Nainal 这是一种解决方法。

正如我们所经历的,在设备重启后,Android 电视盒不会返回已安装应用程序的使用统计信息。这是我遵循的解决方法。

  • 我们获取已安装应用程序的详细信息,然后仅将上次使用的时间存储在哈希图中。
  • 我们使用自定义比较器来比较上次使用的时间,并对我们的已安装应用程序详细信息列表 (ResolveInfo) 进行排序,以获得最近使用的应用程序列表。
  • 现在为了克服设备重启场景的情况,我们在 app 本地维护一个 Hash map。我们会将已安装 App 的 Last used time 存储为 Long millis (epoch) 。lastTimeUsedMap = new HashMap(); 每次从 UsageStatsManager 获得新数据时,我们都会更新地图。
  • 我们简化了地图对象并将其作为字符串存储在共享首选项中。

  • 重新启动后,我们将在使用情况统计中找到已安装的软件包是否有时间戳。如果没有,我们将从存储的 MAP 中获取时间。

这是代码:

private void SortByRecentlyUsed(final List<ResolveInfo> info, final Context context){

    /*load LastTimeUsedMap from pref*/
    lastTimeUsedMap = loadLastTimeUsedMap(context);

    UsageStatsManager usm = getUsageStatsManager(context);
    Calendar calendar = Calendar.getInstance();
    long endTime = calendar.getTimeInMillis();
    calendar.add(Calendar.MONTH, -1);
    long startTime = calendar.getTimeInMillis();

    final List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_BEST, startTime, endTime);

    /*Update AppRecentTimeUsedMap with latest data from UsageStatsManager*/
    updateAppRecentTimeUsedMap(usageStatsList,info);

    class RecentUseComparator implements Comparator<ResolveInfo> {
        @Override
        public int compare(ResolveInfo lhs, ResolveInfo rhs) {
            String lhsPackageName=lhs.activityInfo.applicationInfo.packageName;
            String rhsPackageName=rhs.activityInfo.applicationInfo.packageName;
            long lhsUsedTime = getLastUsedTime(lhsPackageName);
            long rhsUsedTime = getLastUsedTime(rhsPackageName);
            return (lhsUsedTime > rhsUsedTime) ? -1 : (lhsUsedTime == rhsUsedTime) ? 0 : 1;
        }

        private long getLastUsedTime(String packageDetails) {
            long appRecentUsedtime = -1;
            if (appRecentTimeUsedMap.containsKey(packageDetails)) {
                appRecentUsedtime = appRecentTimeUsedMap.get(packageDetails);
                }
            return appRecentUsedtime;
        }
    }
    RecentUseComparator mRecentComp = new RecentUseComparator();
    Collections.sort(info, mRecentComp);
    /*Save the updated  LastTimeUsedMap in pref*/
    saveLastTimeUsedMap(lastTimeUsedMap, context);

}

private void updateAppRecentTimeUsedMap(List<UsageStats> usageStatsList,List<ResolveInfo> info){

    String packageName=null;
    if (usageStatsList != null) {
        for (ResolveInfo Rinfo : info) {
            packageName = Rinfo.activityInfo.applicationInfo.packageName;
            boolean added = false;
            for (UsageStats usageStats : usageStatsList) {
                if (packageName.equalsIgnoreCase(usageStats.getPackageName())) {
                    appRecentTimeUsedMap.put(usageStats.getPackageName(), usageStats.getLastTimeUsed());
                    updateLastTimeUsedMap(usageStats.getPackageName(), usageStats.getLastTimeUsed());
                    added=true;
                }
            }
            if (!added && lastTimeUsedMap.containsKey(packageName)) {
                appRecentTimeUsedMap.put(packageName, lastTimeUsedMap.get(packageName));
            }
        }

    }

}

private void updateLastTimeUsedMap(String packageName,Long timeStamp){
    lastTimeUsedMap.put(packageName, timeStamp);
}

/**
 * Return Map containing Package name and recent used time from preference
 *
 * @param context
 * @return Map<String,Long>
 */
private Map<String,Long> loadLastTimeUsedMap(Context context){
    Map<String,Long> outputMap = new HashMap<String,Long>();
    SharedPreferences pSharedPref = context.getSharedPreferences(LAST_TIME_USED_PREFS, Context.MODE_PRIVATE);
    try{
        if (pSharedPref != null){
            String jsonString = pSharedPref.getString(LAST_TIME_USED_MAP, (new JSONObject()).toString());
            JSONObject jsonObject = new JSONObject(jsonString);
            Iterator<String> keysItr = jsonObject.keys();
            while(keysItr.hasNext()) {
                String key = keysItr.next();
                Long value = jsonObject.getLong(key);
                outputMap.put(key, value);
            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return outputMap;
}
/**
 * Save the updated map containing Package name and recent used time in preference
 *
 * @param inputMap
 * @param context
 */
private void saveLastTimeUsedMap(Map<String,Long> inputMap, Context context){
    final SharedPreferences sharedPreferences = context.getSharedPreferences(LAST_TIME_USED_PREFS,Context.MODE_PRIVATE);
    if (sharedPreferences != null){
        JSONObject jsonObject = new JSONObject(inputMap);
        String jsonString = jsonObject.toString();
        final SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(LAST_TIME_USED_MAP).commit();
        editor.putString(LAST_TIME_USED_MAP, jsonString);
        editor.commit();
    }
}
于 2019-05-08T12:42:03.507 回答