Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

process - How to get recent tasks on Android "L"?

Background

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

The problem

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:

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.

Same is written when using ADT to show the documentation of this function (not currently available on the 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.

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.

What I've tried

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

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

The question

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

Maybe it's possible with root? Or BusyBox ?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

To get top packagename:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...