检测没有得到应用上下文每个触摸事件? [英] Detect every touch events without getting Application Context?

查看:153
本文介绍了检测没有得到应用上下文每个触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以通过类似环境中使用getWindow()获取每个活动触摸事件:

We can get Touch events for each Activity by using getWindow() on context like :

//set Window.Callback for getting touch event 
        final Window window = context.getWindow();
        final Window.Callback localCallback = window.getCallback();
        window.setCallback(new MyWindowCallback(localCallback));

我们

如何能在不使用情况下实现呢?

How can we achieve it without using context ?

有什么办法消除这种回调(因为窗口类没有任何删除回调方法?

Is there any way to remove this callback (Because window class don't have any remove callback methods ?

推荐答案

有一种方法可以让应用程序上下文没有传递给它。我已经使用在生产环境中此code和这工作得很好。

There's a way to get Application context without passing it. I have used this code in production environment and this works fine.

private static Application getApplicationContext() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Context context;
    final Class<?> activityThreadClass =
            Class.forName("android.app.ActivityThread");
    final Method method = activityThreadClass.getMethod("currentApplication");
    context = (Application) method.invoke(null, (Object[]) null);
    Log.d(tag_, "Context is " + context);
    application = (Application)context;
    return application;
}

这篇关于检测没有得到应用上下文每个触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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