gpt4 book ai didi

javascript - 无法捕获 Android Tv webview 应用程序上的键码(HTML+Javascript)

转载 作者:行者123 更新时间:2023-11-28 06:49:12 24 4
gpt4 key购买 nike

我正在尝试为我的网站开发一个 Android 电视应用程序。经过研究后,我开始使用 web-view 来加载包含 html-5 和 JavaScript 的 Web 应用程序。

该应用程序在 WebView 上加载良好,我什至可以在电视模拟器上正确导航,但一旦我在 Nexus Player 上部署该应用程序,该应用程序就不会接受 Enter 事件。当我点击游戏 Controller 上的按钮 A 时,点击 Nexus Player 附带的游戏 Controller 上的 A 按钮时会出现错误 ------"您没有可以执行此操作的应用

之后,我创建了一个虚拟应用程序来在屏幕上显示键事件以进行调试,并且在捕获所有键事件的情况下工作得很好。

我发现,因为它是一个基于 htnl5 和 javascript 的应用程序,所以 NP 不兼容。我对此进行了很多研究,但似乎找不到任何可靠的东西。确实需要一个指针才能使其正常工作。

以下是加载 web 应用程序的 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 Controller 获取关键事件的代码

@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 方法中显示任何 toast,但当我从应用程序退出 Keycode_Back 事件(为 4)以及除 BACK 事件之外的任何其他 keyevent 时,它会显示 toast在 Nexus 播放器或模拟器上都没有按照上面的代码捕获它或显示任何 toast --真的让我发疯。

任何可以为我指明正确方向的建议将不胜感激。

编辑1:我现在可以在 logcat 中获取 keyevnts,但它仍然不显示 Toast,并且我仍然不断收到“您没有可以在单击按钮 A 上执行此操作的应用程序

最佳答案

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

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

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

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView. You can retrieve WebSettings with getSettings(), then enable JavaScript with setJavaScriptEnabled().

关于javascript - 无法捕获 Android Tv webview 应用程序上的键码(HTML+Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com