微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Xamarin Android上实现下载侦听器界面

如何解决在Xamarin Android上实现下载侦听器界面

直到今天,我从未想过要从Urls下载文件,需要在其Activity类上实现Interface Download Listener。我在Xamarin Android上有一个webview,但似乎无法下载文件。 认为这是因为我尚未在我的课程上实现此接口Download Listener。.因此,我尝试了一下,但似乎无法将Interface实现方法OnDwnloadStart方法连接起来,当我请求从以下位置下载时,我认为网页,则IDownloadListener方法不执行任何操作,因为它没有代码...。但是应该处理下载请求的代码在带有OnDownloadStart参数的url,Contentdisposition and mimetype方法中,任何调用该接口的帮助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,mimetype));
            DownloadManager manager = 
       (DownloadManager)GetSystemService(Android.App.Application.DownloadService);
            manager.Enqueue(request);
            //Notify if success with broadCast Receiver
        }
}

代码的哪个部分运行错误?任何帮助表示赞赏。

解决方法

您不需要创建新的IDownloadListener接口,IDownloadListener接口是IDownloadListener下的系统namespace 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,long contentLength)
    {
        throw new NotImplementedException();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。