C# 在Xamarin Android上实现下载侦听器接口
直到今天,我从未想过要从URL下载文件,需要在活动类上实现接口下载侦听器。我有一个Xamarin Android的网络视图,我似乎无法下载文件。。 我想这是因为我没有在我的类上实现这个接口下载侦听器。所以我尝试了一下,但我似乎无法将接口实现方法与C# 在Xamarin Android上实现下载侦听器接口,c#,android,xamarin,webview,C#,Android,Xamarin,Webview,直到今天,我从未想过要从URL下载文件,需要在活动类上实现接口下载侦听器。我有一个Xamarin Android的网络视图,我似乎无法下载文件。。 我想这是因为我没有在我的类上实现这个接口下载侦听器。所以我尝试了一下,但我似乎无法将接口实现方法与OnDwnloadStart方法连接起来,我想当我请求从网页下载时,IDownloadListener方法什么都不做,因为它没有代码……但是应该处理下载请求的代码位于带有url、ContentDisposition和mimetype参数的OnDownlo
OnDwnloadStart
方法连接起来,我想当我请求从网页下载时,IDownloadListener方法什么都不做,因为它没有代码……但是应该处理下载请求的代码位于带有url、ContentDisposition和mimetype
参数的OnDownloadStart
方法中,任何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
}
}
class Internet:AppCompatActivity,IDownloadListener
{
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(这个,savedInstanceState);
SetContentView(Resource.Layout.Browser);
//网络视图定义
webview=this.findviewbyd(Resource.Id.webview1);
//网络视图属性
SetWebViewClient(新的WebViewClient());
WebSettings-WebSettings=webview.Settings;
webSettings.SetSupportMultipleWindows(真);
webSettings.SetEnableSmoothTransition(true);
webSettings.JavaScriptEnabled=true;
webSettings.DomStorageEnabled=true;
webSettings.AllowFileAccessFromFileURLs=true;
//webSettings.JavaScriptEnabled。
SetDownloadListener(这个);
}
//接口下载侦听器方法
公共接口IDownloadListener:Android.Runtime.IJavaObject,IDisposable
{
//我这里什么都没有,这就是我认为需要称之为下载开始的地方
}
//实现OnDownloadStart方法
public void OnDownloadStart(字符串url、字符串userAgent、字符串contentDisposition、字符串mimetype、长contentLength)
{
DownloadManager.Request=newdownloadmanager.Request(Android.Net.Uri.Parse(url));
字符串cookies=CookieManager.Instance.GetCookie(url);
AddRequestHeader(“cookie”,cookies);
AddRequestHeader(“用户代理”,userAgent);
request.SetDescription(“正在将文件下载到crn文件夹…”);
SetTitle(URLUtil.GuessFileName(url、contentDisposition、mimetype));
request.AllowScanningByMediaScanner();
request.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);
File dest=新文件(Android.OS.Environment.RootDirectory+“下载”);
如果(!dest.Exists())
{
如果(!dest.Mkdir())
{Log.Debug(“TravellerLog::,“创建映像文件夹时出现问题”);}
}
request.setdestinationnexternalfilesdir(Application.Context,“下载”,
GuessFileName(url,contentDisposition,mimetype));
下载管理器=
(DownloadManager)GetSystemService(Android.App.Application.DownloadService);
排队(请求);
//通知广播接收器是否成功
}
}
这段代码的哪一部分运行错误?感谢您的帮助。您无需创建新的
IDownloadListener
界面,IDownloadListener
界面是名称空间Android.Webkit
下的系统IDownloadListener
:
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();
}
}
您不需要定义自己的IDownloadListener接口,您需要提供it@Jason,我该怎么做?删除
公共接口IDownloadListener…
关于方法
,OnDownloadStart
,我该如何实现它?您已经有了它的实现。如果您不了解C#接口是如何工作的,请花时间阅读文档。它正在下载,但我似乎找不到下载文件的位置,你能在我发布的代码中检查我的文件逻辑吗?您是否正确使用SetDestinationNexternalFilesDir?第二个参数是dirType,第三个参数是subPath。下面是“+”下载“部分的功能是什么?setdestinationnexternalfilesdir(Application.Context,“download”,URLUtil.GuessFileName(url,contentDisposition,mimetype))的功能是什么
do?我需要将文件保存到内部存储器的方法