#author("2024-07-21T18:29:03+08:00","default:Admin","Admin")
#author("2024-07-21T21:38:45+08:00","default:Admin","Admin")
[[Android]]

&color(red){※前提条件:本情報はAndroid Studio 4.2.2を基づいて説明してる};

#contents

保证URL不会在系统的浏览器中打开
* 保证URL不会在系统的浏览器中打开 [#z005272a]

#codeprettify{{
WebView webView = root.findViewById(R.id.webViewHome);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
}}

* WebView与网页数据交互 [#t25627e6]

#codeprettify{{
// 打开开关,设置与Js交互的权限
webSettings.setJavaScriptEnabled(true);
WebView.addJavascriptInterface(new JSBridge(),"bridge");
}}

#codeprettify{{
public class JSBridge {
    // @JavascriptInterface 必须加上
    @android.webkit.JavascriptInterface
    public void print(String msg) {
        // 在合适的地方,比如点击事件处理中
        android.content.Intent intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse("douyin://")); 
        // 替换为实际的抖音唤起链接
        System.out.println(msg);
        try {
            startActivity(intent);
        } catch (Exception ex) {
            // 处理无法唤起的情况,比如提示用户未安装等
            //System.out.println(msg);
            ex.printStackTrace();
        }
    }
}
}}

html里面
#codeprettify{{
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script>
        function callAndroid(){
            bridge.print("传去android的消息");
        }
    </script>
</head>
<body>
<button type="button" id="button1" onclick="callAndroid()">点击按钮调用</button>
</body>
</html> 

}}

#hr();
コメント:
#comment_kcaptcha

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS