我正在尝试使用最近在 Android 中引入的 UsageStatsManager 类读取应用程序上次使用的时间。
但是有一个问题。它为大多数应用程序提供了正确的结果。但是对于某些呼叫应用程序:appUsageStats.get(appInformation.packageName).getLastTimeUsed()
它返回一个不正确的 4 位毫秒。
附件是评估窗口的屏幕截图,显示 8738 为真正的呼叫者应用程序。这是一个错误还是我做错了什么?
注意:作为观察,当mLastTimeSystemUsed
与mLastTimeUsed
. 仅供参考。
下面的代码片段:
Map<String, UsageStats> appUsageStats = UStats.getMapOfAggregatedUsage(MainActivity.this);
if(appUsageStats.get(appInformation.packageName)!=null) {
long used = appUsageStats.get(appInformation.packageName).getLastTimeUsed();
long installedDiff2 = new Date().getTime() - new Date(used).getTime();
long daysGap = TimeUnit.DAYS.convert(installedDiff2, TimeUnit.MILLISECONDS);
if(daysGap == 0) {
appInfoObj.setAppUsedDate("Used : Today");
} else {
appInfoObj.setAppUsedDate("Used : " + String.valueOf(daysGap) + " days ago");
}
appInfoObj.setAppUsedDateNumber(daysGap);
}
Run Code Online (Sandbox Code Playgroud)
UStats 类方法:
public static Map<String, UsageStats> getMapOfAggregatedUsage(Context context) {
Calendar calendar = Calendar.getInstance();
long …
Run Code Online (Sandbox Code Playgroud)