如何在webview中下载文件..?

时间:2017-04-17 03:59:27

标签: c# android xamarin webview download-manager

我的应用程序将打开浏览器下载文件,但我希望它在webview中下载(不要打开浏览器下载)。 我不擅长app,请尽可能给我完整的代码。

 using System;
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    using Android.Webkit;

    using System.Net;
    namespace REC
    {
        [Activity(Label = "ABC", MainLauncher = true, Icon = "@drawable/rec512", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]

        public class MainActivity : Activity
        {

            private WebView mWebView;
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);

                // Set our view from the "main" layout resource
                RequestWindowFeature(WindowFeatures.NoTitle);
                SetContentView(Resource.Layout.Main);

                mWebView = FindViewById<WebView>(Resource.Id.webview);
                mWebView.Settings.SetRenderPriority(WebSettings.RenderPriority.High);
                mWebView.Settings.JavaScriptEnabled = true;
                mWebView.SetWebChromeClient(new WebChromeClient());
                mWebView.LoadUrl("http://www.mywebsite");
                mWebView.SetDownloadListener(new MyDownloadListerner(this));
                mWebView.SetWebViewClient(new WebViewClient());




            }









            class MonkeyWebChromeClient : WebChromeClient
            {
                public override bool OnJsAlert(WebView view, string url, string message, JsResult result)
                {
                    return base.OnJsAlert(view, url, message, result);
                }

                public override Boolean OnJsConfirm(WebView view, String url, String message, JsResult result)
                {
                    return base.OnJsConfirm(view, url, message, result);
                }

                public override Boolean OnJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result)
                {
                    return base.OnJsPrompt(view, url, message, defaultValue, result);
                }
            }

            public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
            {
                if (keyCode == Keycode.Back && mWebView.CanGoBack())
                {
                    mWebView.GoBack();

                    return true;
                }

                return base.OnKeyDown(keyCode, e);
            }





        }
        public class WebClient : WebViewClient
        {

            public override bool ShouldOverrideUrlLoading(WebView view, string url)
            {



                    //return base.ShouldOverrideUrlLoading(view, url);
                    view.LoadUrl(url);
                    return true;


            }

            internal object GetWebRequest(Uri address)
            {
                throw new NotImplementedException();
            }
        }




class MyDownloadListerner : Java.Lang.Object, IDownloadListener
    {
        Context cont;
        public MyDownloadListerner(Context context)
        {
            cont = context;
        }
        public void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
        {
            Android.Net.Uri uri = Android.Net.Uri.Parse(url);
            Intent intent = new Intent(Intent.ActionView, uri);
            cont.StartActivity(intent);
        }
    }





}

必要的消息  一个必要的消息  一个必要的消息  必要的消息

0 个答案:

没有答案