Android 中哪些函数或代码需要 GET_TASKS 权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25151420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:09:13  来源:igfitidea点击:

What functions or codes require GET_TASKS permission in Android?

androidpermissions

提问by Nizzy

I think the GET_TASKS permission is an orphan line in my AndroidManifest.xml. I want to remove it safely. Do you know any function or code that requires this permission? Thank you.

我认为 GET_TASKS 权限是我的 AndroidManifest.xml 中的一个孤立行。我想安全地删除它。您知道任何需要此权限的函数或代码吗?谢谢你。

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

采纳答案by 19greg96

From the android reference

来自 android参考

Allows an application to get information about the currently or recently running tasks.

允许应用程序获取有关当前或最近运行的任务的信息。

An example is public List<ActivityManager.RecentTaskInfo> getRecentTasks (int maxNum, int flags)as it throws SecurityException if the caller does not hold the GET_TASKS permission.

一个例子是public List<ActivityManager.RecentTaskInfo> getRecentTasks (int maxNum, int flags),如果调用者不持有 GET_TASKS 权限,它会抛出 SecurityException。



Note that according to the documentation

请注意,根据文档

This constant was deprecated in API level 21. No longer enforced.

此常量在 API 级别 21 中已弃用。不再强制执行。

and

As of LOLLIPOP, this method is no longer available to third party applications: 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.

从 LOLLIPOP 开始,此方法不再适用于第三方应用程序:引入以文档为中心的最近记录意味着它可能会将个人信息泄露给调用者。为了向后兼容,它仍将返回其数据的一小部分:至少是调用者自己的任务(尽管请参阅 getAppTasks() 以获取检索该信息的正确支持方式),并且可能还有一些其他任务,例如已知的 home不要敏感。

回答by Abhishek

class CheckRunningActivity extends Thread{
ActivityManager am = null;
Context context = null;

public CheckRunningActivity(Context con){
    context = con;
    am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}

public void run(){
    Looper.prepare();

    while(true){
        // Return a list of the tasks that are currently running,
        // with the most recent being first and older ones after in order.
        // Taken 1 inside getRunningTasks method means want to take only
        // top activity from stack and forgot the olders.
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

        String currentRunningActivityName = taskInfo.get(0).topActivity.getClassName();

        if (currentRunningActivityName.equals("PACKAGE_NAME.ACTIVITY_NAME")) {
            // show your activity here on top of PACKAGE_NAME.ACTIVITY_NAME
        }
    }
    Looper.loop();
}
}