c# - 在 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
}
}
此代码的哪一部分运行错误?任何帮助表示赞赏..
解决方案
你不需要创建一个新的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, string userAgent, string contentDisposition, string mimetype, long contentLength)
{
throw new NotImplementedException();
}
}
推荐阅读
- node.js - 博客提交功能将帖子保存到数据库,但不会在角度组件中显示博客帖子
- python - selenium webdriver .click() 不工作
- java - JSON 解析错误 - 无法调用“com.fasterxml.jackson.databind.JsonDeserializer.getObjectIdReader()”,因为“valueDes”为空
- r - 线性模型循环
- git - git log --oneline 不起作用,不显示任何内容
- c# - Entity Famework 从对象创建列
- reactjs - 页面加载和页面刷新时 Gatsby 闪烁 (FOUC)
- android - AIDE如何在手机上运行gradle
- recursion - 在 UML 序列图上的对象内调用私有方法
- python - Json 数据不会在一天结束时保存