我已经构建了在xamarin中使用webview的应用程序,并且我已经在包括Galaxy S7边缘在内的许多设备上测试了它们,因为我认为它不起作用的原因是因为Edge屏幕。但是,它在S7 Edge上工作正常,但在我试过的S8 Edge设备上,它们会立即崩溃。
我使用的只有两个类:
MainActivity:
namespace APPNAME.Android
{
[Activity(Label = "APPNAME", Icon = "@drawable/icon", MainLauncher =
false,
ConfigurationChanges = ConfigChanges.ScreenSize |
ConfigChanges.Orientation)]
public class MainActivity : Activity
{
WebView web_view;
//private bool appeared;
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string
url)
{
view.LoadUrl(url);
return false;
}
}
protected override void OnCreate(Bundle bundle)
{
Window.RequestFeature(WindowFeatures.NoTitle);
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
web_view = FindViewById<WebView>(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.SetWebViewClient(new HelloWebViewClient());
web_view.LoadUrl("http://link.com");
}
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back && web_view != null)
{
try
{
if (web_view.CanGoBack())
{
Log.Debug("StackOverflow", "Allow browser back");
// Toast.MakeText(this, "Going back",
ToastLength.Short).Show();
web_view.GoBack();
return true;
}
}
catch (Exception ex)
{
Log.Error("StackOverflow", ex.Message);
}
}
{
//Log.Error("StackOv20erflow", "Null webview...");
//StartActivity(typeof(MainActivity));
//Finish();
OnBackPressed();
}
//Log.Debug("StackOverflow", "Back button blocked");
//Toast.MakeText(this, "Back button blocked",
ToastLength.Short).Show();
return false;
}
public override void OnBackPressed()
{
Intent startMain = new Intent(Intent.ActionMain);
startMain.AddCategory(Intent.CategoryHome);
startMain.SetFlags(ActivityFlags.NewTask);
StartActivity(startMain);
}
}
}
启动画面:
namespace APPNAME.Android
{
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory =
true)]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
public override void OnCreate(Bundle savedInstanceState,
PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
Log.Debug(TAG, "SplashActivity.OnCreate");
}
// Launches the startup task
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
// Simulates background work that happens behind the splash screen
async void SimulateStartup()
{
//Log.Debug(TAG, "Performing some startup work that takes a bit of
time.");
//await Task.Delay(1000); // Simulate a bit of startup work.
//Log.Debug(TAG, "Startup work is finished - starting
MainActivity.");
//StartActivity(new Intent(Application.Context,
typeof(MainActivity)));
await Task.Delay(4000);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
StartActivity(intent);
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionName="1.0.0.8" package="APPNAME.android"
android:installLocation="auto" android:versionCode="8">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="25" />
<application android:label="APPNAME" android:icon="@drawable/Icon"
android:debuggable="false"></application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
不知道从哪里开始解决问题,因为我不知道是什么原因造成的。
任何帮助将不胜感激!
答案 0 :(得分:1)
除非在事件处理程序 中,否则请避免使用async void
。而且永远不要创建new Task
并使用task.Start
参考Async/Await - Best Practices in Asynchronous Programming
那就是说我会建议你在SplashActivity
public class SplashActivity : AppCompatActivity {
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
base.OnCreate(savedInstanceState, persistentState);
Log.Debug(TAG, "SplashActivity.OnCreate");
}
// Launches the startup task
private event EventHandler Starting = delegate { }
protected override void OnResume() {
base.OnResume();
//subscribe to event
Starting += Activity_Starting;
//raise event
Starting (this, EventArgs.Empty);
}
// Simulates background work that happens behind the splash screen
async void Activity_Starting(object sender, EventArgs e) {
//unsubscribe
Starting -= Activity_Starting;
//and carry on
//Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
//await Task.Delay(1000); // Simulate a bit of startup work.
//Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
//StartActivity(new Intent(Application.Context, typeof(MainActivity)));
await Task.Delay(4000);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
StartActivity(intent);
}
}
}