android – 检测每个触摸事件,而不会覆盖dispatchTouchEvent()?

前端之家收集整理的这篇文章主要介绍了android – 检测每个触摸事件,而不会覆盖dispatchTouchEvent()?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发需要跟踪使用它的应用程序上的所有触摸事件的 Android库.现在用户应该在他们的每个活动中覆盖他们的Activity的dispatchTouchEvent().如下所示
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    MyLib.dispatchTouchEvent(event);
    return super.dispatchTouchEvent(event);
}

但是Appsee通过Appsee.start(“key”)检测触摸事件; onCreate方法集成在应用程序启动的第一个活动中.这怎么可能.任何帮助?

解决方法

回答这可能对他人有帮助.答案将有助于需要拦截触摸事件的库.

如果你不想覆盖dispatchTouchEvent()(Imp,如果你是编程库)方法的活动,那么我们可以使用Window.Callback类.创建您自己的Window.Callback实现作为下面的代码,然后将其设置为从您的活动回调Window对象.

在下面的代码片段中,为您获取的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);

    }
}
原文链接:https://www.f2er.com/android/310731.html

猜你在找的Android相关文章