Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何计算用户在android中打开应用程序的时间?_Java_Android_Time_Code Analyst - Fatal编程技术网

Java 如何计算用户在android中打开应用程序的时间?

Java 如何计算用户在android中打开应用程序的时间?,java,android,time,code-analyst,Java,Android,Time,Code Analyst,我正在尝试实现一些东西,允许我计算用户使用应用程序的时间,但编写代码似乎相当困难或至少很复杂。我尝试了几次尝试和逻辑,但都失败了 我不确定这是否是写的,但我创建了两个变量,如下所示 static Date startTime = new Date(); The startTime variable goes in the main activity and it notes the time user launched the app. static Date endTim

我正在尝试实现一些东西,允许我计算用户使用应用程序的时间,但编写代码似乎相当困难或至少很复杂。我尝试了几次尝试和逻辑,但都失败了

我不确定这是否是写的,但我创建了两个变量,如下所示

    static Date startTime = new Date();
The startTime variable goes in the main activity and it notes the time user launched the app.


     static Date endTime = new Date();
The endTime goes in the onDestroy method and takes the time when the user closes the app.

If this approach is write, I understand I would need to find out the different between the two times but I am not sure how to attempt something like this. Could someone please help, what I want to be able to do is get a final value and pass it to the code below and send it over to Google Analyst for Android;

/*
 * Called after a list of high scores finishes loading.
 *
 * @param loadTime The time it takes, in milliseconds, to load a resource.
 */
public void onLoad(long loadTime) { // I need to put my value instead of loadTime, but I cannot figure out how to find the difference between two times.

  // May return null if EasyTracker has not been initialized with a property
  // ID.
  Tracker easyTracker = EasyTracker.getInstance(this);

  easyTracker.send(MapBuilder
      .createTiming("resources",    // Timing category (required)
                    loadTime,       // Timing interval in milliseconds (required)
                    "high scores",  // Timing name
                    null)           // Timing label
      .build()
  );
}
谢谢,我真的非常感谢你的帮助

endTime.getTime() - startTime.getTime()
这将获得两个日期之间的毫秒数。

请参阅