简体   繁体   English

如何在android中以编程方式跟踪应用程序使用时间?

[英]How to track app usage time programmatically in android?

I want to know how much time user is using my app .我想知道用户使用我的应用程序的时间。 So whenever he opens my app I want to know time and whenever he close app i want to know time.所以每当他打开我的应用程序时,我想知道时间,每当他关闭应用程序时,我想知道时间。 When user will close app i want to send usage time to server.当用户关闭应用程序时,我想将使用时间发送到服务器。 How can i track usage time in android ?我如何在 android 中跟踪使用时间?

The way I mentioned below is a very efficient way to get the total spent a time of an app by a user in android.我在下面提到的方式是一种非常有效的方式来获取用户在 android 中花费的总时间。 Actually I also use this approach to reward my users for their time spent.实际上,我也使用这种方法来奖励我的用户花费的时间。 First of all, you need to a custom ActivityLifecycleCallbacks which is responsible to trigger out when the application began to start and when stop.首先,您需要一个自定义的ActivityLifecycleCallbacks ,它负责在应用程序开始启动和停止时触发。 You need to just register the ActivityLifecycleCallbacks to your application instance.您只需将ActivityLifecycleCallbacks注册到您的应用程序实例。 This will also care about if any user pauses the app so you don't need to think about it.The custom ActivityLifecycleCallbacks class is -这也将关心是否有任何用户暂停应用程序,因此您无需考虑它。自定义ActivityLifecycleCallbacks类是 -

public class ApplicationTimeSpentTracker implements Application.ActivityLifecycleCallbacks {
    private int activityReferences = 0;
    private boolean isActivityChangingConfigurations = false;
    private long startingTime;
    private long stopTime;
   /**startingTime is the UNIX timestamp (though I increased readability by converting into millisecond to second) your app is being foregrounded to the user and stopTime is when it is being backgrounded (Paused)*/
    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
        if (++activityReferences == 1 && !isActivityChangingConfigurations) {
        
            // App enters foreground
            startingTime=System.currentTimeMillis()/1000; //This is the starting time when the app began to start
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityStopped(Activity activity) {
        isActivityChangingConfigurations = activity.isChangingConfigurations();
        if (--activityReferences == 0 && !isActivityChangingConfigurations) {
        
            // App enters background
            stopTime = System.currentTimeMillis() / 1000;//This is the ending time when the app is stopped 
            
            long totalSpentTime= stopTime-startTime; //This is the total spent time of the app in foreground. Here you can post the data of total spending time to the server.
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }
}

Finally you need to register the callback to your application instance in MainActivity like this -最后,您需要像这样在MainActivity中将回调注册到您的应用程序实例 -

    activity.getApplication().registerActivityLifecycleCallbacks(new ApplicationTimeSpentTracker());

Here is my post if you need details如果您需要详细信息,这是我的帖子

I would put data loges onResume and onPause methods.我会将数据日志放在onResumeonPause方法中。 and on every onPause Append usage time to SharedPrefrences or some database and on requirements on create or when internet is turned on i would send usage statistics.并且在每个 onPause 将使用时间附加到SharedPrefrences或某些数据库以及创建或打开互联网时的要求时,我会发送使用情况统计信息。

In this manner you can track every activities usage.通过这种方式,您可以跟踪每个活动的使用情况。

Not sure if some of the box methods exists.不确定是否存在某些框方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM