「Android」获取前台应用信息

获取前台应用信息

    /**
     * 获取前台应用信息
     *
     * @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

  1. 聊天框的图片预览页:com.tencent.mm.ui.chatting.gallery.ImageGalleryUI
  2. 聊天框的文档预览页:com.tencent.mm.pluginsdk.ui.tools.MiniQBReaderUI
  3. 朋友圈:com.tencent.mm.plugin.sns.ui.SnsTimeLineUI
  4. 朋友圈的图片预览页:com.tencent.mm.plugin.sns.ui.SnsBrowseUI

QQ

QQ包名:com.tencent.mobileqq

  1. 聊天框的图片预览页:com.tencent.mobileqq.richmediabrowser.AIOGalleryActivity
  2. 聊天框的文档预览页:com.tencent.mobileqq.filebrowser.FileBrowserActivity

你可能感兴趣的:(android)