跟踪应用程序的运行时间

时间:2015-03-04 22:05:32

标签: android android-package-managers

我的代码将显示在设备上运行的所有应用程序(包名称和应用程序名称)。 我需要时间使用多长时间。我怎么能这样做?请。

     ActivityManager manager =(ActivityManager)getSystemService(ACTIVITY_SERVICE);
    final List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
    StringBuilder b = new StringBuilder();
    PackageManager pm = this.getPackageManager();
    for (ActivityManager.RunningAppProcessInfo process: runningProcesses)
    {
        try
        {
            b.append (pm.getApplicationLabel(pm.getApplicationInfo(process.processName, PackageManager.GET_META_DATA)));
        }
        catch (Exception e)
        {
            b.append("Unknown");
        }
        b.append(System.getProperty("line.separator"));
        b.append(process.processName);
        b.append(System.getProperty("line.separator"));
        //here the run time of an application
        b.append(System.getProperty("line.separator"));
        b.append("---------------------------------------------------");
        b.append(System.getProperty("line.separator"));
    }
    processesShow=(TextView) this.findViewById(R.id.citac);
    processesShow.setText(b.toString());

1 个答案:

答案 0 :(得分:0)

如果您想在应用程序打开时跟踪时间,可以使用计时器

如果您想查看应用程序安装后多长时间,请使用日期

1.检查安装应用程序的日期

2.存储

3.当用户打开应用时,请检查当前日期

4.比较它并将文本设置为多长时间

使用和比较日期的链接很少

http://www.mysamplecode.com/2011/10/android-get-current-time-and-date.html

Best way to compare dates in Android

如果你想使用计时器,你应该这样做:

    public class MainActivity extends ActionBarActivity {

    TextView timer;
    private long startTime = 0L;
    private Handler customHandler = new Handler();
    long timeInMilis = 0L;
    long timeSwapBuff = 0L;
    long updateTime = 0L;
    int p= 0;
    int S_secs_int;
    String s_p;
    String S_secs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button = (ImageView) findViewById(R.id.button);
        timer = (TextView) findViewById(R.id.timer);
        checkTime();




    }


    public  void checkTime(){


            p++;
            points.setText(""+p);

            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

    }

    private Runnable updateTimerThread = new Runnable() {
        @Override
        public void run() {

            timeInMilis = SystemClock.uptimeMillis() - startTime;
            updateTime= timeSwapBuff + timeInMilis;

            int secs = (int) (updateTime/1000);
            int mins = secs /60;
            secs = secs % 60;

            int milliseconds = (int) (updateTime % 1000);
            S_secs = String.format("%05d",secs);

            timer.setText(S_secs);

            customHandler.postDelayed(this, 0);

        }
    };

  }