退出YouTube全屏后退按钮Cordova关闭应用程序

时间:2019-10-08 00:00:48

标签: javascript android cordova

我以全屏模式播放YouTube视频,当我按“后退”按钮时,它将关闭该应用程序,而不是退出全屏模式。我正在使用 JavaScript和适用于Android的Cordova 6.3.1 。这是我的代码:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
   document.addEventListener("backbutton", function (e) {
     e.preventDefault();
       screen.orientation.lock('portrait');
     }, false);
}

以前的代码无法正常工作。

谢谢。

1 个答案:

答案 0 :(得分:1)

我还没有找到答案,但是到目前为止,我仍在发布我的发现,希望对大家有所帮助。

版本:cordova-android 8.1.0

我发现了什么

问题来自以下事实:要全屏显示Youtube视频,SystemWebChromeClient会调用CordovaWebViewImpl.showCustomView()方法。这样,新视图似乎在Cordova能够onDispatchKeyEvent()并将backbutton事件发送到JavaScript端之前捕获了Back按钮事件。

这里是一些日志,为清楚起见添加了注释

// Video is fullscreen, and then dismissed with the onscreen minimize button
D/CordovaWebViewImpl: showCustomView : showing Custom View + view = android.widget.FrameLayout{a7cc344 V.E...... ......I. 0,0-0,0}
D/CordovaWebViewImpl: hideCustomView : Hiding Custom View : mCustomView = android.widget.FrameLayout{a7cc344 V.E...... .......D 0,0-1080,1704}

// While the video is not displayed fullscreen, the back button event is correctly caught and sent to the JS side
D/CordovaWebViewImpl: onDispatchKeyEvent : isBackButton = true mCustomView = null

// Once again in fullscreen, then click on the back button
D/CordovaWebViewImpl: showCustomView : showing Custom View + view = android.widget.FrameLayout{fa6d336 V.E...... ......I. 0,0-0,0}
// No back button event caught
// A destroy lifecycle event is sent (from the video FrameLayout handling?) and handleDestroy() is called
D/CordovaWebViewImpl: handleDestroy : load about:blank
D/CordovaWebViewImpl: >>> loadUrl(about:blank)
W/cr_AwContents: WebView.destroy() called while WebView is still attached to window.
D/CordovaWebViewImpl: hideCustomView : Hiding Custom View : mCustomView = android.widget.FrameLayout{a7cc344 V.E...... .......D 0,0-1080,1704}

我尝试过的

  • 创建FrameLayout的子类,并为其覆盖onKeyDown()onKeyUp(),并在showCustomerView()中使用它

视频不再显示。

文档