我在通过WebView下载时遇到问题,并找到以下代码:
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
问题是,我的WebView没有功能setDownladListener()...我也看到他们使用了WebView.loadUrl(),而我也没有,我有WebView.Source ... 为什么我的网络视图不同?以及如何将其更改为?谢谢!
答案 0 :(得分:0)
Xamarin Android WebView.SetDownloadListener()
答案 1 :(得分:0)
在代码中首先使用Android.Webkit.WebView webView;
。
初始化 webView :
webView = FindViewById<WebView>(Resource.Id.webView1);
webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new HelloWebViewClient());
webView.LoadUrl("https://www.xamarin.com/university");
webView.SetDownloadListener(new downloadListener());
这需要创建一个自定义HttpClient( HelloWebViewClient ),这里有LoadUrl
方法:
public class HelloWebViewClient : WebViewClient
{
// For API level 24 and later
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
view.LoadUrl(request.Url.ToString());
return false;
}
}
如果要 SetDownloadListener ,则需要创建IDownloadListener
:
public void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
{
DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(url));
request.AllowScanningByMediaScanner();
request.SetNotificationVisibility(DownloadManager.Request.VisibilityVisibleNotifyCompleted);
request.SetTitle("Download finish");
request.SetAllowedOverMetered(true);
request.SetVisibleInDownloadsUi(true);
request.SetAllowedOverRoaming(true);
//this file path need dynamic request permission
//String fileName = URLUtil.GuessFileName(url, contentDisposition, "Downloadfile");
//request.SetDestinationInExternalPublicDir(Application.Context.GetExternalFilesDir("Download").ToString() , fileName);
DownloadManager downloadManager = DownloadManager.FromContext(this);
long downloadId = downloadManager.Enqueue(request);
}