Search Unity

Android - creating an override plugin

Discussion in 'Editor & General Support' started by GonzoCubFan, Jan 20, 2014.

  1. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    I've read the docs on how to create an override plugin for Android, and I've been semi-successful. I have actually gotten the onCreate call to override properly and display a log message as indicated in the docs. However, I can't seem to get an override of either dispatchKeyEvent() or onKeyDown() to work at all. It seems that the override function never gets called.

    I'm hoping that someone who has had success with this might be able to point out the error of my ways.

    Here is my java code:

    Code (csharp):
    1.  
    2. package com.gonzo.testoverride;
    3.  
    4. import com.unity3d.player.UnityPlayerActivity;;
    5.  
    6. import android.os.Bundle;
    7. import android.util.Log;
    8. import android.view.KeyEvent;
    9.  
    10. public class testOverride extends UnityPlayerActivity
    11. {
    12.     protected void onCreate(Bundle savedInstanceState)
    13.     {
    14.  
    15.         // call UnityPlayerActivity.onCreate()
    16.         super.onCreate(savedInstanceState);
    17.  
    18.         // print debug message to logcat
    19.         Log.d("OverrideActivity", "onCreate has been called!");
    20.     }
    21.  
    22.     @Override
    23.     public boolean dispatchKeyEvent( KeyEvent event )
    24.     {
    25.         Log.d( "OverrideActivity", "dispatchKeyEvent called!" );
    26.         return super.dispatchKeyEvent( event );
    27.     }
    28.  
    29.     @Override
    30.     public boolean onKeyDown( int keyCode, KeyEvent event )
    31.     {
    32.         Log.d( "OverrideActivity", "onKeyDown called!" );
    33.         return super.onKeyDown( keyCode, event );
    34.     }
    35. }
    36.  
    Note that I have tried this both with and without the "@Override" with the exact same results. What you see above is the latest attempt.

    Here is the AndroidManifest.xml that I'm using.

    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gonzo.testoverride">
    4.   <application android:icon="@drawable/app_icon" android:label="@string/app_name">
    5.     <activity android:name="com.unity3d.player.UnityPlayerActivity" android:launchMode="singleTask" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    6.     </activity>
    7.     <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:launchMode="singleTask" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    8.       <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
    9.     </activity>
    10.     <activity android:name="com.gonzo.testoverride.testOverride"
    11.               android:label="@string/app_name"
    12.               android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    13.         <intent-filter>
    14.             <action android:name="android.intent.action.MAIN" />
    15.             <category android:name="android.intent.category.LAUNCHER" />
    16.         </intent-filter>
    17.     </activity>
    18.   </application>
    19. </manifest>
    20.  
    Once again, the log message from onCreate shows up just fine, but none of the messages from either onKeyDown() or displatchKeyEvent() ever appear - and yes, I am typing on the keyboard into a GUI.TextField.

    Thanks in advance for any help or insight you can offer. I'm not a Java mavin by any means, and I've only gotten this far by a painful about of hacking. Android is not my favorite platform, primarily because of the incredibly lax standards that they impose on soft keyboard implementations. That, along with Unity's washing their hands of the whole raft of keyboard issues that they choose to ignore rather than address, has soured me considerably on Android right now. Hopefully that will change some day...

    = Ed =
     
    Last edited: Jan 20, 2014
  2. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70