获取前台应用的打开活动 [英] Get open activity of the foreground app

查看:49
本文介绍了获取前台应用的打开活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取前台应用程序的当前活动.我尝试遵循 这个答案,后来使用了 这个库(因为第一个解决方案仅适用于 Lollipop),并且它们确实在告诉前台应用程序的包名称方面工作得很好,但我需要知道哪些活动是打开的.

I need to get the current Activity of the foreground App. I tried following this answer and later used this library(because the first solution only worked from Lollipop), and them indeed worked well telling the foreground app's package name, but I need to know what activity is open.

推荐答案

嘿@Capitan Luzzatto,

Hey @Capitan Luzzatto,

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

首先你需要 2 个权限来读取任务

First you need 2 permissions to read the tasks

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

并且您应该首先检查用户 API 级别是否为 21 或更高,如果是,您应该获得如下使用权限设置的访问权限

And you should check first the user API level is 21 or more if yes you should get access for usage access settings like below

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;
}

最后你可以使用以下方法获得最新的前台应用

Finally you can get the latest Foreground app using the following method

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;
}

要获取前台活动,请使用这样的方法

To get Foreground activity use the method like this

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 方式获取设备最近的屏幕,也许我们应该对此进行更多调查,不确定它是否适用于所有设备,但几乎适用于所有设备

AndroidUtils.java - To get the device recent screen in Hack way, Maybe we should investigate more on this, Not sure it will work on all device, but it works on almost all devices

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;
    }
}

这篇关于获取前台应用的打开活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆