测量时间花在Android应用 [英] Measure Time Spent on Android Applications

查看:129
本文介绍了测量时间花在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();

您也许可以看看这个还有:<一href="http://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service">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应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆