如何在android webview中使用Web组件/元素

问题描述 投票:0回答:1

[您好,我是Android开发的新手,正在webview上工作,我已经创建了网络音乐播放器,可以在其中播放和下载音乐。 播放器功能在Web浏览器上运行良好,但在android设备上,下载功能不起作用。

以下代码用于webview-

    WebView webView = findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);

    if (!b1) {
        webView.setVisibility(View.GONE);
    } else {
        webView.setVisibility(View.VISIBLE);
        webView.loadUrl("http://music.in/android_music/music.html");
    }

播放器设计如下。突出显示的下载图标在android webview中不起作用。

enter image description here

我要将文件下载并存储在Music文件夹中。请帮忙。在此先感谢您的帮助...!

android webview android-webview android-download-manager webclient-download
1个回答
0
投票

您可以尝试这种方法

创建这样的Web界面

public class JSInterface {
    Context context;
    JSInterface (Context context) {
        this.context= context;
    }

    @JavascriptInterface
    public void downloadFile(String url) {
     // Do your downloading task 
    }
}

然后像这样在html内定义方法

<input type="button" value="Say hello" onClick="downloadFilefromUrl('url')" />

<script type="text/javascript">
    function downloadFilefromUrl(url) {
        if(typeof Android !== "undefined" && Android !== null) {
            android.downloadFile(url);
        } else {
            alert("error");
        }
    }
</script>
© www.soinside.com 2019 - 2024. All rights reserved.