【发布时间】:2017-03-15 14:57:01
【问题描述】:
我在 xamarin 工作,试图制作一个 Android 项目。
我正在尝试学习 Android,但我有点卡住了。我有一个 MainActivity,它曾经是在应用程序启动时启动的活动。现在我正在添加一个启动屏幕,这意味着另一个活动,但我显然希望这是已启动的活动,并让它在一段时间后启动主要活动。它首先启动了splash活动。好的。当它尝试从启动活动启动主要活动时,它会关闭。我正在使用 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" />
【问题讨论】: