在Android的应用程序内跟踪用户空闲时间 [英] Tracking user idle time within the app in Android

查看:262
本文介绍了在Android的应用程序内跟踪用户空闲时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,目前还没有系统的API,我得到的用户空闲时间。当我说用户的空闲时间,我的意思是用户必须在我的应用程序在触摸屏上的一些互动。所以,我想通过我自己来跟踪它。的方式来达到我的心是扩展活动并覆盖 onuserinteraction 方法来保存最后一个用户的活动时间。

As far as I know, there is no system API for me to get user idle time. When I say user idle time, I mean user have some interaction on the touch screen within my app. Therefore, I want to track it by my self. The way come up to my mind is to extends Activity and override onuserinteraction method to save the last user active time.

但面临的挑战是,我的应用程序具有的多个进程。我不知道下面的路是正确的和有效的方式。

But the challenge is that my app have multiple processes. I am not sure the following way is the correct and efficient way.

我想用共享preference MODE_WORLD_WRITEABLE 标记来存储用户上次活动时间。为避免出现性能问题,我还缓存在每个活动的活动中的最后一个活动的时间。我只新的时间保存到共享prefernces 如果diff时间> 1秒。

I want to use SharedPreference with MODE_WORLD_WRITEABLE flag to store the user last active time. To avoid the performance issue, I also cache the last active time within the activity in each activity. And I only save the new time to SharedPrefernces if the diff time > 1 second.

相比,使用AIDL是这样高效?其实,是AIDL也使用文件共享变量?如果是的话,我觉得这两个方面应该有类似的表现,对不对?谢谢你。

Is this way efficient compared to using aidl? Actually, is aidl also share variable using file? If yes, I think the two ways should have similar performance, right? Thanks.

推荐答案

每一个时间,而不是把它写下来,来自世界各地,使这个在你的应用程序中的全局函数:

Instead writing it down every time, from everywhere, make this a global function in your App:

public class MyApp extends Application {
    private static SharedPreferences sPreference;

    private static final long MIN_SAVE_TIME = 1000;
    private static final String PREF_KEY_LAST_ACTIVE = "last_active";
    private static final String PREF_ID_TIME_TRACK = "time_track";

    public static void saveTimeStamp(){
        if(getElapsedTime() > MIN_SAVE_TIME){
            sPreference.edit().putLong(PREF_KEY_LAST_ACTIVE, timeNow()).commit();
        }
    }

    public static long getElapsedTime(){
        return timeNow() - sPreference.getLong(PREF_KEY_LAST_ACTIVE,0);
    }

    private static long timeNow(){
        return Calendar.getInstance().getTimeInMillis();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sPreference = getSharedPreferences(PREF_ID_TIME_TRACK,MODE_PRIVATE);
    }
}

添加应用程序类来体现:<应用机器人:名称=com.example.MyApp

在一个抽象活动类场所节能功能:

Place saving functionality in an abstract Activity class:

public abstract class TimedActivity extends Activity {

    @Override
    public void onUserInteraction() {
        super.onUserInteraction();
        MyApp.saveTimeStamp();
    }

    public long getElapsed(){
        return MyApp.getElapsedTime();
    }

}

现在,延长从这个类所有的活动,所有的人都将自动保存时间,并能够使用 getElapsed()

Now, extend all your activities from this class, all of them will be auto-save time, and will be able to use getElapsed().

这篇关于在Android的应用程序内跟踪用户空闲时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆