jer*_*710 5 android navigator android-webview
navigator.share()JavaScript 函数在我的 Android 应用程序的 WebView 中不起作用的原因是什么?通过 Web 浏览器访问相同的 URL 时,会弹出本机 Android 共享对话框,但从我的应用程序的 WebView 访问 URL 时不会弹出。
setJavaScriptEnabled设置为true。setDomStorageEnabled也设置为true。最终,我对我的网络视图使用了以下设置。对我来说,它不仅仅是navigator.share,但对我有用的东西可能对其他人也有用。值得一试。
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAllowContentAccess(true);
mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
mWebView.addJavascriptInterface(new JavaScriptShareInterface(), "AndroidShareHandler");
Run Code Online (Sandbox Code Playgroud)
AndroidShareHandler 将成为一个全局 JavaScript 函数,该函数将在 Web 视图中可用,并且可以通过单击按钮来触发,例如:
爪哇:
package com.your.package;
import android.webkit.JavascriptInterface;
public class JavaScriptShareInterface {
@JavascriptInterface
public void share(String url) {
// your share action
}
}
Run Code Online (Sandbox Code Playgroud)
JavaScript:
shareButton.addEventListener('click', () => {
window.AndroidShareHandler.share('https://stackoverflow.com');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4273 次 |
| 最近记录: |