Adobe AIR back button event not working with Andro

2020-04-17 04:05发布

问题:

The key_down event is not being fired and we are not able to prevent the app to be closed. It's happening on Adobe AIR games when we target to Android API 28+ only (everything works fine if the build was targeted to API 27 or lower)

NativeApplication.nativeApplication.addEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler );

function keyDownHandler( event:KeyboardEvent ):void
{
    trace( event.keyCode );
    if (event.keyCode == Keyboard.BACK)
    {
        event.preventDefault();
    }
}

I did some research, I went through some Adobe AIR .jar files and everything looks good. In fact I did some changes to handle the dispatchKeyEvent() through their AndroidActivityWrapper, I received the event and I return 'true' to prevent the app to be closed, but still not working, it's ignoring the event.

I need some clues about what was changed in Android API 28, why the same Adobe AIR sdk fails only with this API. I read that Harman is working on a fix but there are no official information about that.

I provide part of the stacktrace where you can see the dispatchKeyEvent call coming from native android components:

 at com.app.mobile.appMobile.onKeyDown(appMobile.java:834)
 at com.core.ane.Context.onKeyEvent(Context.java:669)
 at com.adobe.air.AndroidActivityWrapper.callInputEventListeners(AndroidActivityWrapper.java:1921)
 at com.adobe.air.AndroidActivityWrapper.dispatchKeyEvent(AndroidActivityWrapper.java:1488)
 at java.lang.reflect.Method.invoke(Native Method)
 at air.com.app.mobile.test.AppEntry.InvokeMethod(AppEntry.java:365)
 at air.com.app.mobile.test.AppEntry.dispatchKeyEvent(AppEntry.java:496)
 at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:563)
 at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:6038)
 at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5893)

回答1:

Finally I found the problem, it's related to the focus not being assigned to AIRWindowSurfaceView when I target the game to Android API 28.

This is the code I have to run when the ANE is initialized in order to restore the focus:

  AndroidActivityWrapper aaw = AndroidActivityWrapper.GetAndroidActivityWrapper();
  aaw.getView().requestFocus();
  aaw.getView().onWindowFocusChanged(true);