+ Reply Thread
Hello I will be sharing a nice tutorial on how to track user activity running time(staying activity on screen) without wasting much time keep start your android studio and create new project with any name of your choice.

We have to create a java variable on activity with name long i,j after doing that we have to add j=System.currentTimeMillis(); onPause and also add i=System.currentTimeMillis(); onResume method after writing d above code on your activity lifecycle, you will have subtract the onpause and resume method and also divide it by 1000.0 to fetch result in millisecond
after doing that u can now start doing your decision making ( if else statement)

To understand my tutorial better check below java code snippet:
package com.mycompany.myapp2; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.util.Log; import android.widget.Toast; import android.widget.*; import java.util.*; public class MainActivity extends Activity { long i,j; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); j=System.currentTimeMillis(); Log.d("Time in pause",""+j); //String fn=""+(j-i)/1000.0; double fn = (j-i)/1000.0; int x = (int) fn; if(x<60){ Toast.makeText(this,"foreground second "+x, 2000).show(); }else if(x>60){ Toast.makeText(this,"foreground minute "+x/60, 2000).show(); }else if(x>3600){ Toast.makeText(this,"foreground hour "+x/3600, 2000).show(); }else if(x>86400){ Toast.makeText(this,"foreground days "+x/86400, 2000).show(); } } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); i=System.currentTimeMillis(); Log.d("Time in resume",""+i);} @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); } }
briefly on how the time was calculated, when you subtract the two variable (j-i) it return result in millisecond and it will be in double variable(double variable will return result with 1.56second) so we have to convert it to int to generate single number(single number 1second) without remainder , to convert millisecond to second you have to divide your result by 1000.0 You can check the below time table.

1000milisecond = 1second
60second = 1minute
60minute = 1hour
24hour = 1day,
You must be familiar with the table, after understand the table we have to convert the day, hour, minute and second to second.

You can actually see the decision making statement starting with if(x<60) if the decision is true it will print out the toast.
To calculate hours in second : 60second*60minute=3600second in minute.
To calculate day in second: 24hours*3600second=86400second in a day.
after writing your decision you should also get the current value of x in toast and divide it by the condition value you use. You can see the below image of the app running.
Note: minimize your android phone to see the result of the app runtime.
Nice tutorial over there... I am really impressed
Can you please use bbcode to format the codes. So they stay distinct from normal text. Just wrapping the whole codes in a
//codes in here
I guess this tutorial isn't for newbies and starters :(
Nice work
Back to top