gpt4 book ai didi

android - 如何在 Android "L"上获取最近的任务?

转载 作者:IT老高 更新时间:2023-10-28 22:20:50 33 4
gpt4 key购买 nike

背景

My app允许按最近启动的时间对应用列表进行排序。

问题

从 Android "L"开始,函数 getRecentTasks将只返回当前应用程序已启动的应用程序列表,如 documentation 中所述:

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

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 显示该函数的文档时也是这样写的(目前在 Internet 上不可用):

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.

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

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

我尝试过的

目前,我只使用启发式方法来了解最近启动了哪些应用程序 - 我得到的是正在运行的进程列表。

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

问题

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

也许有可能用root?还是 BusyBox?

最佳答案

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

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

启动设置操作来直接提示用户进行设置
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 - 如何在 Android "L"上获取最近的任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590533/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com