【发布时间】:2023-03-11 17:01:01
【问题描述】:
我正在开发一个简单的 xamarin android 应用程序,它允许显示响应式网站并接收推送通知。
Webview 似乎对 Xamarin 有一些限制,因此我基于以下 cmets (https://forums.xamarin.com/discussion/3259/cannot-override-webviewclient-class-for-file-upload-support) 使用了以下解决方法 (https://github.com/GabLeRoux/xamarin-android-webview-upload) 以使 html 上传按钮按预期工作。
一切顺利,直到我将推送通知从 GCM 迁移到 FCM。之后webview,重新开始工作。
基本上,html 上传按钮不会打开文件选择器对话框,也不会引发任何错误。它什么都不做。
以下是我在网络活动中使用的代码。
我正在使用 Xamarin.Android 7.3 和 VS 2015。
欢迎任何帮助。
using System;
using Android.Runtime;
using Android.Views;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Webkit;
using Android.Widget;
namespace sigese
{
[Activity(Label = "WebActivity")]
public class WebActivity : Activity
{
IValueCallback mUploadMessage;
private static int FILECHOOSER_RESULTCODE = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.WebLayout);
ActionBar.Hide();
var username = Intent.GetStringExtra("username");
var password = Intent.GetStringExtra("password");
var chrome = new FileChooserWebChromeClient((uploadMsg, acceptType, capture) => {
mUploadMessage = uploadMsg;
var i = new Intent(Intent.ActionGetContent);
i.AddCategory(Intent.CategoryOpenable);
i.SetType("image/*");
StartActivityForResult(Intent.CreateChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
});
var webview = this.FindViewById<WebView>(Resource.Id.LocalWebView);
webview.SetWebViewClient(new WebViewClient());
webview.SetWebChromeClient(chrome);
webview.Settings.JavaScriptEnabled = true;
webview.LoadUrl("https://example.com/login.asp?username="+username+"&password="+password);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage)
return;
Java.Lang.Object result = intent == null || resultCode != Result.Ok
? null
: intent.Data;
mUploadMessage.OnReceiveValue(result);
mUploadMessage = null;
}
}
public override void OnBackPressed()
{
WebView localWebView = FindViewById<WebView>(Resource.Id.LocalWebView);
if (localWebView.CanGoBack())
{
localWebView.GoBack();
}
else
{
return;
}
}
}
partial class FileChooserWebChromeClient : WebChromeClient
{
Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback;
public FileChooserWebChromeClient(Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback)
{
this.callback = callback;
}
// For Android < 5.0
[Java.Interop.Export]
public void openFileChooser(IValueCallback uploadMsg, Java.Lang.String acceptType, Java.Lang.String capture)
{
callback(uploadMsg, acceptType, capture);
}
// For Android > 5.0
}
}
【问题讨论】:
标签: c# visual-studio webview xamarin.android