检测没有得到应用上下文每个触摸事件?上下文、事件

2023-09-06 14:20:40 作者:爱过人渣信过贱人

我们可以通过类似环境中使用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;
}
 
精彩推荐
图片推荐