首页 > 解决方案 > 在 Xamarin Android 上实现下载侦听器接口

问题描述

直到今天,我从没想过要从 Urls 下载文件,需要在他的 Activity 类上实现接口下载侦听器。我在 Xamarin Android 中有一个 webview,我似乎无法下载文件.. 认为那是因为我还没有在我的课堂上实现这个接口下载侦听器..所以我试了一下,但我似乎无法连接接口用该方法实现方法OnDwnloadStart,我想当我从网页请求下载时,IDownloadListener 方法什么也不做,因为它没有代码....但是应该处理下载请求的代码在OnDownloadStart带有url, ContentDisposition and mimetype参数的方法中,任何 hep制作接口调用OnDownloadStart方法肯定会受到赞赏..这是我使用的代码...

class Internet : AppCompatActivity,IDownloadListener
    {
       protected override void OnCreate(Bundle savedInstanceState)
        {

            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.Browser);
           //Webview definition
         webview = this.FindViewById<WebView>(Resource.Id.webview1);
           //webview properties
            webview.SetWebViewClient(new WebViewClient());
            WebSettings webSettings = webview.Settings;
            webSettings.SetSupportMultipleWindows(true);
            webSettings.SetEnableSmoothTransition(true);
            webSettings.JavaScriptEnabled = true;
            webSettings.DomStorageEnabled = true;
            webSettings.AllowFileAccessFromFileURLs = true;
            // webSettings.JavaScriptEnabled.
            webview.SetDownloadListener(this);
      }
     //Interface Download Listener Method
       public interface IDownloadListener : Android.Runtime.IJavaObject, IDisposable
        {
         //I got nothing here and this is what i think needs to call OndownloadStart  
        }
      //implementing OnDownloadStart Method
       public void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
        {
            DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(url));
            String cookies = CookieManager.Instance.GetCookie(url);
            request.AddRequestHeader("cookie", cookies);
            request.AddRequestHeader("User-Agent", userAgent);
            request.SetDescription("Downloading file to crn folder...");
            request.SetTitle(URLUtil.GuessFileName(url, contentDisposition, mimetype));
            request.AllowScanningByMediaScanner();
            request.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);
            File dest = new File(Android.OS.Environment.RootDirectory + "download");
            if (!dest.Exists())
            {
                if (!dest.Mkdir())
                { Log.Debug("TravellerLog ::", "Problem creating Image folder"); }
            }
            request.SetDestinationInExternalFilesDir(Application.Context, "download", 
       URLUtil.GuessFileName(url, contentDisposition, mimetype));
            DownloadManager manager = 
       (DownloadManager)GetSystemService(Android.App.Application.DownloadService);
            manager.Enqueue(request);
            //Notify if success with BroadCast Receiver
        }
}

此代码的哪一部分运行错误?任何帮助表示赞赏..

标签: c#androidxamarinwebview

解决方案


你不需要创建一个新的IDownloadListener界面,IDownloadListener界面是一个系统IDownloadListenernamespace Android.Webkit

namespace Android.Webkit
{
    [Register("android/webkit/DownloadListener", "", "Android.Webkit.IDownloadListenerInvoker", ApiSince = 1)]
    public interface IDownloadListener : IJavaObject, IDisposable, IJavaPeerable
    {
        void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength);
    }
}

任何实现 IDownloadListener 接口的类或结构都必须包含与接口指定的 OnDownloadStart 匹配的 Equals 方法的定义。

所以就像这样:

public class MainActivity : AppCompatActivity, IDownloadListener
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
       SetContentView(Resource.Layout.activity_main);

    }

    public void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
    {
        throw new NotImplementedException();
    }
}

推荐阅读