簡(jiǎn)介
類UsageStatics
是記錄app使用統(tǒng)計(jì)分析數(shù)據(jù)的,比如:最后一是使用的時(shí)間/包名性誉,這個(gè)是需要權(quán)限的
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
效果圖
類和方法
類 | 方法 | 說明 |
---|---|---|
UsageStaticsManager | -- | app使用統(tǒng)計(jì)的管理者 |
-- | queryUsageStats (int intervalType, long beginTime, long endTime) | 按照條件查詢捌臊,結(jié)果是List<UsageStats>杨蛋,API>=21 |
UsageStats | -- | app使用統(tǒng)計(jì)信息類 |
-- | getPackageName() | 獲取包名,API>=21 |
-- | getLastTimeUsed() | 最后的使用時(shí)間理澎,API>=21 |
Java
先要獲取UsageStatsManager
UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
在獲取5年內(nèi)的統(tǒng)計(jì)數(shù)據(jù)逞力,并用RecyclerView顯示
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -5);
// queryUsageStats API>=21 UsageStatsManager.INTERVAL_DAILY
List<UsageStats> usageStatsList = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, calendar.getTimeInMillis(), System.currentTimeMillis());
如果null == usageStatsList || usageStatsList.size() == 0
說明乜有給權(quán)限,需要打開設(shè)置界面
`
[TOC]
``
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
# 知識(shí)點(diǎn) #
## 如果獲取app的logo? ##
根據(jù)`packageName`獲取
Drawable getApplicationIcon (String packageName)
例:
packageManager.getApplicationIcon(packageName())
## 怎么對(duì)List進(jìn)行排序糠爬? ##
Collections.sort(usageStatsList, new ComparatorList());
public class ComparatorList implements Comparator<UsageStats> {
@Override
public int compare(UsageStats t1, UsageStats t2) {
return (int) (t2.getLastTimeUsed() - t1.getLastTimeUsed());
}
}
# 其他 #
google Android Sample:https://developer.android.google.cn/samples/AppUsageStatistics/index.html
Demo:http://git.oschina.net/AndroidBaseDemo/AppUsageStatistics01