Getting the time for each application spent in the foreground in Android

904 Views Asked by At

I'm trying to get the time when the application is in the foreground and I want to use Service while doing this because it needs to run in the background all the time.

This is my main activity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startService(View view) {
        Intent intent = new Intent(this, appTracker.class);
        startService(intent);
    }

    public void stopService(View view) {
        Intent intent = new Intent(this, appTracker.class);
        stopService(intent);
    }
}

And this is the service activity that I will use for this job.

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class appTracker extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "App Track Starting...", Toast.LENGTH_LONG);

        //should I implement this here?
        UsageStats usageStats;
        String PackageName = "Nothing";

        long TimeInforground = 500;

        int minutes = 500, seconds = 500, hours = 500;
        UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService("usagestats");     

        long time = System.currentTimeMillis(); 

        List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
            time - 1000 * 10, time); 

        if (stats != null) {
            SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
            for (UsageStats usageStats : stats) {
                TimeInforground = usageStats.getTotalTimeInForeground();
                PackageName = usageStats.getPackageName();

                minutes = (int)((TimeInforground / (1000 * 60)) % 60);
                seconds = (int)(TimeInforground / 1000) % 60 ;
                hours   = (int)((TimeInforground / (1000 * 60 * 60)) % 24);

                Log.i("BAC", "PackageName is" + PackageName + "Time is: "
                    + hours + "h" + ":" + minutes + "m" + seconds + "s");
            }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

My question is should I implement the code that will get the time for every application in onStartCommand method or somewhere else? Thank you in advance.

1

There are 1 best solutions below

3
On

Application in foreground state means that one of your activities is currently visible to the user

I will go creating a new base Activity like this

public class BaseActivity extends AppCompatActivity {
    ....
    @Override
    public void onResume(){
        //app is showing, start the timer
    }

    @Override
    public void onPause(){
        //app is going to background or an activity switch is occuring, pause the timer
    }
}

Then every Activity you have in the app could extends this BaseActivity

public class MainActivity extends BaseActivity {
    ...
}

Now you need to save the timer property in the SharedPref or somewhere else based on the calculations you need.

BTW: This implementation needs a good understanding of the Android framework so if you are not really familiar with that i will go with some other options.