如何在Android“L"上获取最近的任务? [英] How to get recent tasks on Android "L"?

查看:30
本文介绍了如何在Android“L"上获取最近的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用允许按时间对应用列表进行排序最近推出.

My app allows to sort an apps list by the time they were recently launched .

从 Android "L" 开始,函数 getRecentTasks 只会返回当前应用已启动的应用列表,如 文档:

As of Android "L" , the function getRecentTasks will just return the list of apps that the current app has launched, as written in the documentation:

如果您的应用使用 ActivityManager.getRecentTasks()...

If your app uses ActivityManager.getRecentTasks()...

随着新的并发文件和活动的推出即将发布的版本中的任务功能(请参阅并发文档和下面的最近"屏幕中的活动),ActivityManager.getRecentTasks() 方法现已弃用以改进用户隐私.为了向后兼容,这个方法仍然返回一个其数据的一小部分,包括调用应用程序自己的任务以及可能的其他一些非敏感任务(例如 Home).如果您的应用程序正在使用此方法来检索自己的任务,请使用android.app.ActivityManager.getAppTasks() 而不是检索信息.

With the introduction of the new concurrent documents and activities tasks feature in the upcoming release (see Concurrent documents and activities in Recents screen below), the ActivityManager.getRecentTasks() method is now deprecated to improve user privacy. For backward compatibility, this method still returns a small subset of its data, including the calling application’s own tasks and possibly some other non-sensitive tasks (such as Home). If your app is using this method to retrieve its own tasks, use android.app.ActivityManager.getAppTasks() instead to retrieve that information.

用ADT来显示这个函数的文档时也是这样写的(目前网上没有):

Same is written when using ADT to show the documentation of this function (not currently available on the Internet) :

此方法已弃用.从L开始,此方法不再可用到第三方应用程序:随着以文档为中心的引入最近意味着它可以向呼叫者泄露个人信息.为了向后兼容,它仍然会返回它的一小部分数据:至少调用者自己的任务(尽管参见 getAppTasks()检索该信息的正确支持方式),并且可能其他一些已知不敏感的任务,例如家.

This method is deprecated. As of L, this method is no longer available to third party applications: as the introduction of document-centric recents means it can leak personal information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks (though see getAppTasks() for the correct supported way to retrieve that information), and possibly some other tasks such as home that are known to not be sensitive.

我不明白为什么要采取这种行动,因为很容易看到用户拥有哪些应用程序,即使没有任何许可.

I don't get why this act was taken, as it's easy to see which apps the user has, and even without any permission.

事实上,这是我添加的这个功能的一个很大限制,所以我希望有办法克服这个问题.

Thing is, this is a big restriction for this feature that I've added, so I hope there is a way to overcome this.

目前,我只使用启发式方法来确定最近启动了哪些应用 - 我获取的是正在运行的进程列表.

For now, I only used a heuristic way about which apps were recently launched - I get the list of running processes instead.

我还可以使用流程的重要性值,也许还有importanceReasonComponent",但这只是启发式和猜测......

I could also use the importance value of the processes and maybe the "importanceReasonComponent" , but this is just all heuristics and guesses ...

有没有办法克服这个限制?有什么我没有想到的解决方法吗?

Is there a way to overcome this restriction? Any workaround I haven't thought of?

也许可以用root?还是 BusyBox ?

Maybe it's possible with root? Or BusyBox ?

推荐答案

要获取当前运行的顶级(前台)包名称,您需要使用 Android Lollipop 中的 Usage Stats API.

To get the top (Foreground) package name currently running you need to use the Usage Stats API in Android Lollipop.

但请注意,用户必须在每个应用程序的设置中批准对使用数据的访问.因此,您应该通过使用

But notice user has to approve the access to usage data in the settings for each Application. So you should prompt the user directly to the setting by launching setting action with

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);

获取顶级包名:

String topPackageName ;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService("usagestats");                       
    long time = System.currentTimeMillis(); 
    // We get usage stats for the last 10 seconds
    List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time);                                    
    // Sort the stats by the last time used
    if(stats != null) {
        SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
        for (UsageStats usageStats : stats) {
            mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
        }                    
        if(!mySortedMap.isEmpty()) {
            topPackageName =  mySortedMap.get(mySortedMap.lastKey()).getPackageName();                                   
        }                                       
    }
}

这篇关于如何在Android“L"上获取最近的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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