我正在使用xamarin尝试制作Android项目。
我正在尝试学习Android而且我有点陷入困境。我有一个MainActivity,它用作在app start上启动的活动。现在我添加一个启动画面,这意味着另一个活动,但我显然希望这是已启动的活动,并让它在一段时间后启动主要活动。它首先启动了启动活动。好。当它尝试从启动活动启动主活动时,它会关闭。我正在使用xamarin手册中的一个示例,通过一些小的调整来制作这个启动活动,比如让它继承Activity而不是AppCompActivity。
https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/
请注意,MainActivity已经运行了一段时间,现在它只是崩溃或关闭它不是主要的启动器应用程序活动。
我知道我只是缺少一个重要的条目,它可能与清单文件或我的cs文件中缺少声明有关,因为我发现这就是为什么应用程序通常只是悄然关闭。许多清单声明是在运行时在cs文件中生成的,因为它们来自我见过和使用过的样本。这是我的代码。谁能告诉我我失踪了什么?对于长度感到抱歉,但我不确定要包括或不包括。
这是启动活动。
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
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(2000); // Simulate a bit of startup work.
Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
这是我的MainActivity。请注意,它比在此处发布的更多,正如您将在清单文件中看到的权限所反映的那样,但是在此处发布时间过长,并且此活动始终有效,直到现在必须由另一个活动启动。我可以根据要求添加更多内容,但我认为问题出在其他地方
using System;
using System.IO;
using System.Net;
using System.Text;
using Android.Webkit;
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
using Java.Interop;
using Android.Util;
using MyNameSpace.Services;
using Android.Preferences;
using Android.Graphics.Drawables;
namespace MyNameSpace
{
[Activity(Label = "My Agent", Theme = "@android:style/Theme.NoTitleBar", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.KeyboardHidden)]
public class MainActivity : Activity
{
WebView webview = null;
urlPrefix = "http://example.com/";
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
webview = new WebView(this);
webview.Settings.JavaScriptEnabled = true;
webview.Settings.SetGeolocationEnabled(true);
webview.SetWebViewClient(new MyWebViewClient(this));
webview.SetWebChromeClient(new MyWebChromeClient(this));
SetContentView(webview);
webview.LoadUrl(urlPrefix + "connection.aspx");
//Set Notification bar but make the activity intent pending on user click of notification msg
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);
Notification.Builder builder = new Notification.Builder(this)
.SetContentTitle("My Agent Running")
.SetContentText("Show in forground")
.SetSmallIcon(Resource.Drawable.hippo72)
.SetContentIntent(pendingIntent);
// Build the notification:
notification = builder.Build();
// Get the notification manager:
notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
notificationManager.Notify(notificationId, notification);
public class MyWebViewClient : WebViewClient
{
private readonly Context _context;
public MyWebViewClient(Context context)
{
_context = context;
}
public bool shouldOverrideUrlLoading(WebView view, String url)
{
var uri = Android.Net.Uri.Parse(url);
var intent = new Intent(Intent.ActionView, uri);
_context.StartActivity(intent);
return true;
}
}
public class MyWebChromeClient : WebChromeClient
{
private readonly Context _context;
public MyWebChromeClient(Context context)
{
_context = context;
}
public override void OnGeolocationPermissionsShowPrompt(string origin, GeolocationPermissions.ICallback callback)
{
callback.Invoke(origin, true, false);
}
}
}
我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mydomain.myagent" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />
<application android:label="My Agent" android:icon="@drawable/hippo72"></application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.LOCATION_HARDWARE" />
<uses-permission android:name="android.permission.PERSISTENT_ACTIVITY" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
答案 0 :(得分:0)
在SplashActivity中试用此代码。
使用Finish();没有任务和async / await
[Activity(MainLauncher = true, Theme = "@style/MyTheme.Splash", NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Intent startup = new Intent(this, typeof(MainActivity));
StartActivity(startup);
Finish();
}
}