android activity 檢測每個觸摸事件,而不重寫dispatchTouchEvent()?



android override dispatchtouchevent (1)

回答這可能會對其他人有幫助。 答案將有助於需要攔截觸摸事件的圖書館。

如果您不想覆蓋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);

    }
}

我正在開發Android庫,它需要跟踪使用它的應用程序上的所有觸摸事件。 現在,用戶應該在他們的每個活動上覆蓋他們的dispatchTouchEvent() 。 如下所示

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

Appsee通過Appsee.start(“key”);檢測觸摸事件Appsee.start(“key”); onCreate方法集成在應用程序啟動的First activity中。 這怎麼可能。 任何幫助?





touch-event