Android 使用UsageStatsManager获取前台A
2021-07-01 本文已影响0人
yunhen
private long mCurrTime = 0;
private String mForegroundAppPkg = null;
private SortedMap<Long, UsageStats> mSortedMap = null;
private TreeMap<Long, UsageEvents.Event> mUsageEventMap = null;
private List<UsageStats> usageStatsList = null;
/**
* 查询应用使用情况
* @return 得到最后一个触发 ACTIVITY_RESUMED 的 pkgname
*/
public String getForegroundAppPkg() {
final String t_ = "getForegroundAppPkg() ";
if (destroyed) {
return "";
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
mCurrTime = System.currentTimeMillis();
UsageStatsManager usageStatsManager = ((UsageStatsManager) mContext.getSystemService(Context.USAGE_STATS_SERVICE));
if (usageStatsManager == null) {
L.d(TAG, t_ + "1 usageStatsManager == null ");
return null;
}
long beginTime = mCurrTime - 1000 * 60 * 60 * 22;
// We get usage stats for the last 10 seconds
if (usageStatsList != null)
usageStatsList.clear();
usageStatsList = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, beginTime, mCurrTime);
// Sort the stats by the last time used
if (usageStatsList != null && usageStatsList.size() > 0) {
//Collections.synchronizedSortedMap
if (mSortedMap == null)
mSortedMap = (new TreeMap<Long, UsageStats>());
mSortedMap.clear();
for (UsageStats usageStats : usageStatsList) {
mSortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
/**
* 最后使用的activity的包名
*/
String lastActivityUsedPkg = mSortedMap.get(mSortedMap.lastKey())
.getPackageName();
L.d(TAG, t_ + " lastActivityUsedPkg = " + lastActivityUsedPkg);
UsageEvents usageEvents = usageStatsManager
.queryEvents(beginTime, mCurrTime);
if (usageEvents == null) {
L.d(TAG, t_ + "3 usageEvents == null ");
return "";
}
if (mUsageEventMap == null)
mUsageEventMap = Maps.newTreeMap();
mUsageEventMap.clear();
/**
* 数字标识, 大于0 表示此app在前台,等于 0 ,在后台
*/
int foregroundNum = 0;
UsageEvents.Event eventTemp = null;
// get last event
while (usageEvents.hasNextEvent()) {
eventTemp = new UsageEvents.Event();
usageEvents.getNextEvent(eventTemp);
if (eventTemp == null || eventTemp.getPackageName() == null)
continue;
if (! eventTemp.getPackageName().equals(lastActivityUsedPkg)) {
continue;
} else {
int eventType = eventTemp.getEventType();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (eventType == UsageEvents.Event.ACTIVITY_RESUMED) {
foregroundNum++;
} else if (eventType == UsageEvents.Event.ACTIVITY_PAUSED) {
foregroundNum--;
if (foregroundNum < 0)
foregroundNum = 0;
}
} else if (eventType == UsageEvents.Event.MOVE_TO_FOREGROUND) {
foregroundNum++;
} else if (eventType == UsageEvents.Event.MOVE_TO_BACKGROUND) {
foregroundNum--;
if (foregroundNum < 0)
foregroundNum = 0;
}
if (BuildConfig.DEBUG) {
mUsageEventMap.put(eventTemp.getTimeStamp(), eventTemp);
}
}
}
if (BuildConfig.DEBUG){
long lastKey = mUsageEventMap.lastKey();
long firstKey = mUsageEventMap.firstKey();
L.d(TAG, t_ + " getNextEvent eventPkg lastPkg = " + mUsageEventMap.get(lastKey).getPackageName()
+ ",firstPkg = " + mUsageEventMap.get(firstKey).getPackageName()
+ ", lastKey = " + lastKey
+ ",firstKey = " + firstKey
+ ", lastKeyType = " + mUsageEventMap.get(lastKey).getEventType()
+ ", firstKeyType = " + mUsageEventMap.get(firstKey).getEventType());
}
if (foregroundNum > 0) {
mForegroundAppPkg = lastActivityUsedPkg;
}
} else {
L.d(TAG, t_ + "2 usageStatsList == null ");
return null;
}
}
return mForegroundAppPkg;
}