获取前台应用的开放活动

时间:2017-06-05 12:31:21

标签: android service foreground

我需要获取前台App的当前Activity。我尝试跟随this answer并稍后使用this library(因为第一个解决方案仅在Lollipop上运行),并且它们确实很好地告诉了前台应用程序的包名,但我需要知道哪些活动是打开的。< / p>

2 个答案:

答案 0 :(得分:3)

嘿@Capitan Luzzatto,

  • Pre LOLLIPOP(API 21)我们可以从ActivityManager
  • 获取正在运行的应用程序列表
  • 但是从LOLLIPOP我们应该只从UsageStatsManager
  • 获得

首先,您需要2个权限才能阅读任务

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />

如果是,您应首先检查用户API级别是否为21或更高,以获取下面的使用权限设置

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP && !mIsChecked) {
        if (getUsageStatsList(getActivity()).isEmpty()) {

            Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            startActivity(intent);
        }
 }

获取使用情况统计信息列表

    public static List<UsageStats> getUsageStatsList(Context context){
    UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats");
    Calendar calendar = Calendar.getInstance();
    long endTime = calendar.getTimeInMillis();
    calendar.add(Calendar.DATE, -1);
    long startTime = calendar.getTimeInMillis();

    List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,startTime,endTime);

    return usageStatsList;
}

最后,您可以使用以下方法获取最新的Foreground应用

public String getRecentApps(Context context) {
    String topPackageName = "";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);

        long time = System.currentTimeMillis();

        UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 30, System.currentTimeMillis() + (10 * 1000));
        UsageEvents.Event event = new UsageEvents.Event();
        while (usageEvents.hasNextEvent()) {
            usageEvents.getNextEvent(event);
        }

        if (event != null && !TextUtils.isEmpty(event.getPackageName()) && event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
            if (AndroidUtils.isRecentActivity(event.getClassName())) {
                return event.getClassName();
            }
            return event.getPackageName();
        } else {
            topPackageName = "";
        }
    } else {
        ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;

        if (AndroidUtils.isRecentActivity(componentInfo.getClassName())) {
            return componentInfo.getClassName();
        }

        topPackageName = componentInfo.getPackageName();
    }


    return topPackageName;
}

要获取Foreground活动,请使用此方法

public String getRecentActivity(Context context) {
    String topActivityName = "";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);

        long time = System.currentTimeMillis();

        UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 30, System.currentTimeMillis() + (10 * 1000));
        UsageEvents.Event event = new UsageEvents.Event();
        while (usageEvents.hasNextEvent()) {
            usageEvents.getNextEvent(event);
        }

        if (event != null && !TextUtils.isEmpty(event.getPackageName()) && event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
            return event.getClassName();
        } else {
            topActivityName = "";
        }
    } else {
        ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;

        topActivityName = componentInfo.getClassName();
    }


    return topActivityName;
}

AndroidUtils.java - 为了以Hack方式获取设备最近的屏幕,也许我们应该对此进行更多调查,不确定它是否适用于所有设备,但它适用于几乎所有设备

public class AndroidUtils {
    private static final String RECENT_ACTIVITY;

    static {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            RECENT_ACTIVITY = "com.android.systemui.recents.RecentsActivity";
        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
            RECENT_ACTIVITY = "com.android.systemui.recent.RecentsActivity";
        } else {
            RECENT_ACTIVITY = "com.android.internal.policy.impl.RecentApplicationDialog";
        }
    }

    /**
     * To get the Device recent screen activity
     *
     * @param className
     * @return activity
     */
    public static boolean isRecentActivity(String className) {
        if (RECENT_ACTIVITY.equalsIgnoreCase(className)) {
            return true;
        }

        return false;
    }
}

答案 1 :(得分:1)

由于安全问题,您无法执行此操作。您不希望其他应用知道您当前的公开活动。所以其他应用并不希望你监视它们。这是操作系统限制。