如何获得在Android&QUOT近期任务; L"? [英] How to get recent tasks on Android "L"?

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

问题描述

我的应用允许排序,他们的时间的应用程序列表最近推出的。

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

作为机器人的L时,功能<一href="http://developer.android.com/reference/android/app/ActivityManager.html#getRecentTasks%28int,%20int%29">getRecentTasks将刚刚返回的应用程序,目前的应用程序已经启动,作为写在<一个列表href="https://developer.android.com/$p$pview/api-overview.html#Behaviors">documentation:

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()方法现在是很precated改善   用户的隐私。为了向后兼容,此方法仍然返回   小的子集,其数据,包括调用应用程序的自己   任务和可能的一些其它非敏感的任务(如,首页)。如果   您的应用程序使用此方法来获取自己的任务,使用   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) :

此方法去precated。为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?

也许这是可能的根源在哪里?还是BusyBox的?

Maybe it's possible with root? Or BusyBox ?

推荐答案

要获得前(前景)软件包名称当前正在运行,你需要使用使用统计API。在Android的棒棒糖。

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

不过,通知用户批准访问使用数据中的每个应用程序的设置。 所以,你应该用启动设置的动作直接PROMT用户设置

But notice user has to approve the access to usage data in the settings for each Application. So you should promt 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 != null && !mySortedMap.isEmpty()) {
            topPackageName =  mySortedMap.get(mySortedMap.lastKey()).getPackageName();                                   
        }                                       
    }
}

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

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