获取前台利用信息
/**
* 获取前台利用信息
*
* @return 前台利用信息(蕴含前台利用包名和前台利用类名)*/
public ForegroundInfo getForegroundInfo() {if (!isStatAccessPermissionSet()) {
// 疏导用户关上权限
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
sContext.startActivity(intent);
return null;
}
long time = System.currentTimeMillis();
long duration = 600 * 1000;
UsageStatsManager usageStatsManager = (UsageStatsManager) sContext.getSystemService(Context.USAGE_STATS_SERVICE);
UsageEvents usageEvents = usageStatsManager.queryEvents(time - duration, time);
UsageEvents.Event event = new UsageEvents.Event();
String foregroundPackageName = "";
String foregroundClassName = "";
while (usageEvents.hasNextEvent()) {usageEvents.getNextEvent(event);
if (event.getEventType() == UsageEvents.Event.ACTIVITY_RESUMED) {if (TextUtils.equals(sContext.getPackageName(), event.getPackageName())) {continue;}
foregroundPackageName = event.getPackageName();
foregroundClassName = event.getClassName();}
}
if (foregroundPackageName.isEmpty() || foregroundClassName.isEmpty()) {return null;}
return new ForegroundInfo(foregroundPackageName, foregroundClassName);
}
常见 App 的利用信息
微信
微信包名:com.tencent.mm
- 聊天框的图片预览页:com.tencent.mm.ui.chatting.gallery.ImageGalleryUI
- 聊天框的文档预览页:com.tencent.mm.pluginsdk.ui.tools.MiniQBReaderUI
- 朋友圈:com.tencent.mm.plugin.sns.ui.SnsTimeLineUI
- 朋友圈的图片预览页:com.tencent.mm.plugin.sns.ui.SnsBrowseUI
QQ 包名:com.tencent.mobileqq
- 聊天框的图片预览页:com.tencent.mobileqq.richmediabrowser.AIOGalleryActivity
- 聊天框的文档预览页:com.tencent.mobileqq.filebrowser.FileBrowserActivity