在检测的Andr​​oid用户活动 [英] Detecting user activity in android

查看:92
本文介绍了在检测的Andr​​oid用户活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测什么时候是最后一次用户交互的界面。我没有兴趣做任何恶意软件/间谍软件的东西,只需要计算多少时间,因为用户最后一次点击屏幕上的已经过去。

I would like to detect when was the last time the user interacted with the screen. I'm not interested in doing any malware/spyware stuff, just need to calculate how much time has elapsed since the last time the user tapped on the screen.

我们的目标是实现一个功能性的类似于的到一个键盘锁的。

The goal is to achieve a functionality similar to that of a keyguard.

我一直在做一些研究和遵循一些以Q&安培; A在网站上(如<一个href="http://stackoverflow.com/questions/5689591/android-best-way-to-detect-and-handle-user-inactivity">Android按用户检测一般用途:检测和处理用户不活动, Android的最佳途径等等),但是当涉及到​​检测的Andr​​oid用户交互我还没有找到我所需要的。

I've been doing some research and following some to Q&A on the site (such as Android Best Way to Detect and Handle User INACTIVITY, Android: Detect General Use by User among others) but when it comes to detect user interaction in android I haven't found what I need.

在此先感谢。

推荐答案

在一些更多的研究和测试我已经成功地上升了一个解决方案。

After some more research and testing I've managed to rise with a solution.

我所做的就是创造条件,使我得到的窗口管理器的一个实例后台服务,并补充说,得到的用户通知的零大小的视图 倒是在它之外......但作为视图大小为零它可以被调用每次。 为此,我们可以检查<一href="https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html">https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html

What I did was to create a background service in which I get an instance of the WindowManager and add a zero sized view that get's notified of user touches outside of it... but as the view has zero size it can get called every time. For that we may check https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html

InactivityService.java 类检测用户每次点击屏幕,并将其写入到preferences因此它可以显示或更高版本的应用程序的其他组件使用的时间。 这是有效的发现,我们可以播放时间,或者将它传递给我们的应用程序对象或任何其他的解决方案适合或需要。

The InactivityService.java class detects every time the user taps the screen and write it to the preferences so it can be displayed or employed later in other components of the app. It's valid to notice that we can broadcast the time, or pass it to our app object or whatever other solution suits or needs.

/**
 * Detects user taps on screen (user interaction).
 * 
 * @author Junior Buckeridge A.
 *
 */
public class InactivityService extends Service {

    protected static final String LOG_TAG = InactivityService.class.getSimpleName();
    private boolean isAdded = false;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if(!isAdded){
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    0, 0, 0, 0,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    PixelFormat.TRANSLUCENT);
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            View view = new View(this);
            view.setOnTouchListener(new OnTouchListener() {

                @SuppressLint("DefaultLocale")
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    GregorianCalendar calendar = new GregorianCalendar();
                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(InactivityService.this);
                    String log = prefs.getString(LOG_KEY, "");
                    log = String.format("User interaction detected at: %02d:%02d:%02d \n%s", 
                                calendar.get(Calendar.HOUR_OF_DAY),
                                calendar.get(Calendar.MINUTE),
                                calendar.get(Calendar.SECOND),
                                log);
                    prefs.edit().putString(LOG_KEY, log).commit();

                    return false;
                }
            });
            wm.addView(view, params);
        }

        return START_STICKY;
    }
}

我们应该添加以下权限清单:

We should add the following permission to the manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

另外有效提及的是这种方法,结合得到与正在运行的任务的 ActivityManager 是一个非常有效的方法,以确定用户是否正在与手机相互作用

Also valid to mention that this method, combined with getting the running tasks with ActivityManager is a very effective way to determine if the user is interacting with the handset.

这篇关于在检测的Andr​​oid用户活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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