直到今天,我从未想过要从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, contentDisposition, mimetype));
DownloadManager manager =
(DownloadManager)GetSystemService(Android.App.Application.DownloadService);
manager.Enqueue(request);
//Notify if success with BroadCast Receiver
}
}
该代码的哪个部分运行错误?任何帮助表示赞赏。
答案 0 :(得分:1)
您不需要创建新的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();
}
}