查看用户最近执行的Android任务

发布于 2024-10-21 07:44:11 字数 497 浏览 3 评论 0原文

我想查看我的 Android 手机最近的任务。我正在尝试来自互联网的一些代码,但它们都不能正常工作。我只想获取用户最后执行的应用程序的PID和名称。例如,如果我执行计算器应用程序,然后执行我创建的最近任务应用程序,则该应用程序应该能够告诉我类似以下内容:“您执行的最后一个应用程序是‘计算器’,PID 是‘2222’ ”。

我在 Android 开发人员网页上检查了一些代码,这就是我发现的,但我不知道如何在 Android 上实现。

ActivityManager.RecentTaskInfo 您可以检索有关用户最近启动或访问的任务的信息。

ActivityManager.RunningServiceInfo 您可以检索有关系统中当前运行的特定服务的信息。

任何建议,

最好的问候

ActivityManager.RunningTaskInfo 您可以检索有关系统中当前“运行”的特定任务的信息。

I would like to watch the recent task of my android phone. I was trying some code from internet but non of them work properly. I just want to get the PID and Name of the last application executed by the user. For example, if I execute the calculator application and after that the recent task application that I made, this application shoul be able to tell me something like: "the last application you've executed is 'calculator' and the PID is '2222'".

I was checking on Android developers web page for some code and this is what I found, but I don't know how to implement for Android.

ActivityManager.RecentTaskInfo Information you can retrieve about tasks that the user has most recently started or visited.

ActivityManager.RunningServiceInfo Information you can retrieve about a particular Service that is currently running in the system.

any suggestion,

Best regards

ActivityManager.RunningTaskInfo Information you can retrieve about a particular task that is currently "running" in the system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人如玉 2024-10-28 07:44:11
int numberOfTasks = 1;
ActivityManager m = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
//Get some number of running tasks and grab the first one.  getRunningTasks returns newest to oldest
RunningTaskInfo task = m.getRunningTasks(numberOfTasks).get(0);

//Build output
String output  = "the last application you've executed is '"+task.id+"' and the PID is '"+task.baseActivity.toShortString()+"'";

getRunningTasks

RunningTaskInfo

int numberOfTasks = 1;
ActivityManager m = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
//Get some number of running tasks and grab the first one.  getRunningTasks returns newest to oldest
RunningTaskInfo task = m.getRunningTasks(numberOfTasks).get(0);

//Build output
String output  = "the last application you've executed is '"+task.id+"' and the PID is '"+task.baseActivity.toShortString()+"'";

getRunningTasks

RunningTaskInfo

你是年少的欢喜 2024-10-28 07:44:11

这是我的解决方案:

final   ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (int i = 0; i < recentTasks.size(); i++) 
    {
        Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");         
    }

小心!!最后一个ID不是进程的PID!如果你想获取进程的PID,我使用了以下命令:

mLogcatProc = Runtime.getRuntime().exec(new String[] {"ps"}); 

读取结果找到应用程序名称,然后拆分以获得PID进程。

Here is my solution:

final   ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (int i = 0; i < recentTasks.size(); i++) 
    {
        Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");         
    }

Be careful!!The last ID is not the PID of the process!! If you want to get the PID of the process I used the following command :

mLogcatProc = Runtime.getRuntime().exec(new String[] {"ps"}); 

Read the result finding the application name and then split to obtain PID process.

网名女生简单气质 2024-10-28 07:44:11

我最近正在检查可用的服务,该服务也正在使用活动管理器。
可能只需将其更改为 getRecentTasks(int maxNum, int flags) 方法并根据您的需要修改以下内容!?

private boolean isServiceOnline() {

            final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

            final List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
            for (int i = 0; i < services.size(); i++) {
                String packageClassName = services.get(i).service.getClassName();
                if (packageClassName.equals("org.couchdb.android.CouchService")) {
                    Log.d(TAG, "Service Nr. " + i + " :" + services.get(i).service);
                    Log.d(TAG, "Service Nr. " + i + " package name : " + services.get(i).service.getPackageName());
                    Log.d(TAG, "Service Nr. " + i + " class name : " + packageClassName);
                    ;
                    return true;
                }
            }
            return false;
        }

I was recently checking on available services which also was using the activity manager.
Probably just change this to the getRecentTasks(int maxNum, int flags) method and modify the below to your needs!?

private boolean isServiceOnline() {

            final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

            final List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
            for (int i = 0; i < services.size(); i++) {
                String packageClassName = services.get(i).service.getClassName();
                if (packageClassName.equals("org.couchdb.android.CouchService")) {
                    Log.d(TAG, "Service Nr. " + i + " :" + services.get(i).service);
                    Log.d(TAG, "Service Nr. " + i + " package name : " + services.get(i).service.getPackageName());
                    Log.d(TAG, "Service Nr. " + i + " class name : " + packageClassName);
                    ;
                    return true;
                }
            }
            return false;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文