获取前台应用信息
/**
* 获取前台应用信息
*
* @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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。