Tracking when an Android App is open

525 Views Asked by At

I want to track the usage of my app. Every time it's open on the device it sets a String that it's open for that day. If the user closes the app and opens it again in the same day, new value isn't send to server. So, I'm clean on this part. The part that is unknown to me is how can I test when the app is open. One solution is when the app goes on my home activity, but in that case if someone opens something and press back it will process some data as the app is open for the first time, so it's not the best solution.

3

There are 3 best solutions below

2
On

You could create a custom application like so :

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Put your code here 
    }
}

and in your manifest do :

<application
        android:name=".MyApplication"
        <!-- other fields -->

but if you want a better solution you could use a third party analytics API

2
On

When Activity.onCreate() is called, it sends something to the server. Later the server removes duplicates. Fini....

1
On

You can do the following:

  1. Check if string is send for current day by checking the TIMESTAMP stored in SharedPreferences.
  2. If not then, send the string.
  3. Then store the current TIMESTAMP to SharedPreferences.