gpt4 book ai didi

android - 在不覆盖 dispatchTouchEvent() 的情况下检测每个触摸事件?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:40 24 4
gpt4 key购买 nike

我正在开发 Android 库,它需要跟踪使用它的应用程序上的所有触摸事件。现在,用户应该在他们的每个 Activity 中覆盖他们的 Activity 的 dispatchTouchEvent()。如下图

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
MyLib.dispatchTouchEvent(event);
return super.dispatchTouchEvent(event);
}

但是Appsee使用 Appsee.start(“key”); 在应用程序启动的第一个 Activity 中集成的 onCreate 方法上检测触摸事件。这怎么可能。有帮助吗??

最佳答案

回答,因为这可能对其他人有帮助。回答将有助于需要拦截触摸事件的库。

如果您不想覆盖 Activity 的 dispatchTouchEvent()(如果您正在为库编码)方法,那么我们可以使用 Window.Callback 类。按照下面的代码创建您自己的 Window.Callback 实现,然后将其设置为从您的 Activity 到 Window 对象的回调。

按照下面的代码 fragment 为您获得的 Activity 上下文实例执行操作。

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

MyWindowCallback 将是:

您将在 MyWindowCallback 的 dispatchTouchEventMethod() 上获得 TouchEvent,您可以根据需要在其中使用它们。

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.ActionMode;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;

public class MyWindowCallback implements Window.Callback{

Window.Callback localCallback;

public MyWindowCallback(Window.Callback localCallback) {
this.localCallback = localCallback;
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return localCallback.dispatchKeyEvent(event);
}

@SuppressLint("NewApi")
@Override
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
return localCallback.dispatchKeyShortcutEvent(event);
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
//YOU WILL GET TOUCH EVENTS HERE>> USE AS REQUIRED>>
return localCallback.dispatchTouchEvent(event);
}

@Override
public boolean dispatchTrackballEvent(MotionEvent event) {
return localCallback.dispatchTrackballEvent(event);
}

@SuppressLint("NewApi")
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
return localCallback.dispatchGenericMotionEvent(event);
}

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
return localCallback.dispatchPopulateAccessibilityEvent(event);
}

@Override
public View onCreatePanelView(int featureId) {
return localCallback.onCreatePanelView(featureId);
}

@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
return localCallback.onCreatePanelMenu(featureId, menu);
}

@Override
public boolean onPreparePanel(int featureId, View view, Menu menu) {
boolean ret = localCallback.onPreparePanel(featureId, view, menu);
return ret;
}

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
return localCallback.onMenuOpened(featureId, menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
return localCallback.onMenuItemSelected(featureId, item);
}

@Override
public void onWindowAttributesChanged(WindowManager.LayoutParams attrs) {
localCallback.onWindowAttributesChanged(attrs);
}

@Override
public void onContentChanged() {
localCallback.onContentChanged();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
Log.d("","ttest onfocus changed called");
localCallback.onWindowFocusChanged(hasFocus);
}

@Override
public void onAttachedToWindow() {
localCallback.onAttachedToWindow();
}

@Override
public void onDetachedFromWindow() {
localCallback.onDetachedFromWindow();
}

@Override
public void onPanelClosed(int featureId, Menu menu) {
localCallback.onPanelClosed(featureId, menu);
}

@Override
public boolean onSearchRequested() {
return localCallback.onSearchRequested();
}

@SuppressLint("NewApi")
@Override
public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
return localCallback.onWindowStartingActionMode(callback);
}

@SuppressLint("NewApi")
@Override
public void onActionModeStarted(ActionMode mode) {
localCallback.onActionModeStarted(mode);

}

@SuppressLint("NewApi")
@Override
public void onActionModeFinished(ActionMode mode) {
localCallback.onActionModeFinished(mode);

}
}

关于android - 在不覆盖 dispatchTouchEvent() 的情况下检测每个触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25441531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com