无法在Android电视webview应用程序(HTML + Javascript)上捕获密钥代码

时间:2015-10-14 12:41:43

标签: javascript android webview

我正在为我的网站开发一个Android电视应用程序。在做了研究之后,我开始使用web-view加载我的web应用程序,其中包含html-5和JavaScript。

应用程序在网页视图上正常加载,我甚至可以在电视模拟器上正确导航,但只要我在Nexus播放器上部署应用程序,应用程序就不会执行输入活动 。当我点击游戏控制器上的按钮A 时,它会错误地点击Nexus播放器附带的游戏控制器上的A按钮 ------ #34; 您没有可以执行此操作的应用"

之后我创建了一个虚拟应用程序来显示屏幕上的keyevnts以进行调试,并且工作正常,所有关键事件都被捕获。

我已经弄明白,因为它是一个基于htnl5和javascript的应用程序NP不兼容。 我已经对此进行了很多研究,但由于一切显而易见,因此无法找到任何可靠的东西。真的需要一个指针让它运转起来。

以下是用于加载webapp的webview代码:

public class ExampleActivity extends Activity implements DialogInterface.OnKeyListener
{
    webView = (VideoEnabledWebView)findViewById(R.id.webView);
            mButton1=(Button)findViewById(R.id.btn);

            webView.addJavascriptInterface(new MyJavaScriptInterface(this),"AndroidFunction");
            // Initialize the VideoEnabledWebChromeClient and set event handlers
            View nonVideoLayout = findViewById(R.id.nonVideoLayout); // Your own view, read class comments
            ViewGroup videoLayout = (ViewGroup)findViewById(R.id.videoLayout); // Your own view, read class comments
            //noinspection all
            View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments
            webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
            {
                // Subscribe to standard events, such as onProgressChanged()...
                @Override
                public void onProgressChanged(WebView view, int progress) {
                    // Your code...
                }
            };
            webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback() {
                @Override
                public void toggledFullscreen(boolean fullscreen) {
                    // Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
                    if (fullscreen) {
                        WindowManager.LayoutParams attrs = getWindow().getAttributes();
                        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                        attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                        getWindow().setAttributes(attrs);
                        if (android.os.Build.VERSION.SDK_INT >= 14) {
                            //noinspection all
                            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                        }
                    } else {
                        WindowManager.LayoutParams attrs = getWindow().getAttributes();
                        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                        attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                        getWindow().setAttributes(attrs);
                        if (android.os.Build.VERSION.SDK_INT >= 14) {
                            //noinspection all
                            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                        }
                    }

                }
            });
            webView.setWebChromeClient(webChromeClient);

            // Navigate anywhere you want, but consider that this classes have only been tested on YouTube's mobile site
            webView.loadUrl("http://html5dev.anorakstech.com/iads/");
            mButton1.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    webView.loadUrl("javascript:fillContent()");
                }
            });
}

以下是我用于从nexus播放器附带的Real控制器获取关键事件的代码

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);

        Toast.makeText(ExampleActivity.this, "Key Pressed:"+keyCode, Toast.LENGTH_LONG).show();
        webView.loadUrl("javascript:fillContent()");

        switch(keyCode)
        {
            case KeyEvent.ACTION_DOWN:

                Toast.makeText(ExampleActivity.this, "Key Down"+keyCode, Toast.LENGTH_LONG).show();
                return true;

            case KeyEvent.ACTION_UP:
            Log.v("EA", "Keycode" + event.getKeyCode());
            Toast.makeText(ExampleActivity.this, "Key Released"+event.getKeyCode(), Toast.LENGTH_LONG).show();
            return true;

            case KeyEvent.KEYCODE_ENTER:
                Log.v("EA","Keycode"+event.getKeyCode());
                Toast.makeText(ExampleActivity.this, "Pressed Enter"+event.getKeyCode(), Toast.LENGTH_LONG).show();
                return true;
            case KeyEvent.KEYCODE_DPAD_UP:
                Log.v("EA","Keycode"+event.getKeyCode());
                Toast.makeText(ExampleActivity.this, "Pressed 19", Toast.LENGTH_LONG).show();
                return true;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                Toast.makeText(ExampleActivity.this, "Pressed 20", Toast.LENGTH_LONG).show();
                return true;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                Log.v("EA","Pressed 21");
                Toast.makeText(ExampleActivity.this, "Pressed 21", Toast.LENGTH_LONG).show();
                return true;

            case KeyEvent.KEYCODE_DPAD_RIGHT:
                Toast.makeText(ExampleActivity.this, "Pressed 22", Toast.LENGTH_LONG).show();

                return true;
            case KeyEvent.KEYCODE_CAMERA:
                Toast.makeText(ExampleActivity.this, "Pressed Camera Button", Toast.LENGTH_LONG).show();

                return true;
            case KeyEvent.KEYCODE_1:
                Toast.makeText(ExampleActivity.this, "Pressed 1", Toast.LENGTH_LONG).show();
                return true;
            case KeyEvent.KEYCODE_HOME:
                Toast.makeText(ExampleActivity.this, "Pressed Home Button", Toast.LENGTH_LONG).show();
                return true;

            case KeyEvent.KEYCODE_BACK:
                Toast.makeText(ExampleActivity.this, "Pressed Back Button"+event.getKeyCode(), Toast.LENGTH_LONG).show();
                Intent result = new Intent("Complete");
                setResult(Activity.RESULT_OK, result);
                finish();
                return true;

            case KeyEvent.KEYCODE_BUTTON_A:
                Toast.makeText(ExampleActivity.this, "Pressed 96", Toast.LENGTH_LONG).show();
                return true;
            case KeyEvent.KEYCODE_BUTTON_B:
                Toast.makeText(ExampleActivity.this, "Pressed 97", Toast.LENGTH_LONG).show();
                return true;
            case KeyEvent.KEYCODE_BUTTON_X:
                Toast.makeText(ExampleActivity.this, "Pressed 99", Toast.LENGTH_LONG).show();
                return true;

            case KeyEvent.KEYCODE_BUTTON_Y:
                Toast.makeText(ExampleActivity.this, "Pressed 100", Toast.LENGTH_LONG).show();

                return true;
        }

        return false;
    }

我在这里无法理解的是它没有显示来自keydown方法的任何吐司,但是当我从应用程序退出Keycode_Back事件(4)以及除BACK事件之外的任何其他键事件时它确实显示吐司不会在nexus播放器或模拟器上以上面的代码捕获它或显示任何吐司 - 真的让我发疯。

任何可以指出我正确方向的建议都会非常感激。

编辑1: 我现在可以在logcat中获取keyevnts,但它仍然没有显示Toasts,我仍然不断获得" 你没有一个可以在按钮上执行此操作的应用程序A点击& #34;

1 个答案:

答案 0 :(得分:0)

Are there any other components that may be consuming the key events?

如果没有,请删除super.onKeyDown(keyCode, event);

虽然这可能不是您的问题,from documentation

  

默认情况下,在WebView中禁用JavaScript。您可以通过附加到WebView的WebSettings启用它。您可以使用getSettings()检索WebSettings,然后使用setJavaScriptEnabled()启用JavaScript。