测量时间花在Android应用

编程入门 行业动态 更新时间:2024-10-08 06:18:14
本文介绍了测量时间花在Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新来的机器人。在我的应用程序,我想跟踪多少时间,其他应用程序(这是安装在设备)的使用(在前台)。

I am new to android. In my application, i want to track for how much time other applications (which are installed on device) are used (in foreground).

这可能吗?如果是的话怎么办?

Is it possible? If yes then how?

在此先感谢!

推荐答案

第一件事情,这是需要被称这里是什么是被在前台运行的应用程序:

First thing , that's required to be known here is what are the applications that are running in the foreground :

您可以检测当前与前景/背景的应用程序 ActivityManager.getRunningAppProcesses()电话。

You can detect currently foreground/background application with ActivityManager.getRunningAppProcesses() call.

因此​​,它看起来像::

So, it will look something like ::

class findForeGroundProcesses extends AsyncTask<Context, Void, Boolean> { @Override protected Boolean doInBackground(Context... params) { final Context context = params[0].getApplicationContext(); return isAppOnForeground(context); } private boolean isAppOnForeground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { return false; } final String packageName = context.getPackageName(); for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { return true; } } return false; } } // Now you call this like: boolean foreground = new findForeGroundProcesses().execute(context).get();

您也许可以看看这个还有:Determining前台/后台进程。

You can probably check this out as well : Determining foreground/background processes.

要衡量其运行适当的时候采取的方法的时候,你可以用这个方法:

To measure the time taken by a process to run its due course , you can use this method :

getElapsedCpuTime()

请参阅此文章。

更多推荐

测量时间花在Android应用

本文发布于:2023-11-23 06:47:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620492.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:花在   测量   时间   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!