在我的应用中,我必须获取应用使用情况统计信息,包括应用名称,总前景持续时间和启动计数。但是我无法获得mLaunchCount,让任何人都知道获得价值的替代方法。
以下是获取应用名称和前台时间的代码:
UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(Context.USAGE_STATS_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
List<UsageStats> queryUsageStats = mUsageStatsManager
.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, cal.getTimeInMillis(),
System.currentTimeMillis());
for(int i=0;i<queryUsageStats.size();i++){
UsageStats data = queryUsageStats.get(i);
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName;
try {
appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(data.getPackageName(), PackageManager.GET_META_DATA));
} catch (final PackageManager.NameNotFoundException e) {
appName = "Unknown";
}
str = str.append(appName+" "+data.getTotalTimeInForeground()/1000+"s "+"\n");
}
更新 UsageStats api类的网址