2015-03-23 9 views
0

Я пытаюсь реализовать что-то, что позволяет мне определять время, в течение которого пользователь использовал приложение, но это кажется довольно сложным или, по крайней мере, сложным для кода. Я попробовал несколько попыток и логики, но не смог.Как определить время открытия приложения для Android?

Я не уверен, является ли это писать, но я создаю две переменные, как показано ниже

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() 
); 
} 

Спасибо, и я был бы очень признателен за любую помощь.

+0

См. Https://stackoverflow.com/questions/5911387/difference-in-time-between-two-dates-in-java – childofsoong

ответ

0
endTime.getTime() - startTime.getTime() 

Это даст вам количество миллисекунд между двумя датами.

Смежные вопросы