【发布时间】:2011-07-03 02:02:56
【问题描述】:
如何在程序启动时实现一个简单的“启动画面”?我正在复制一个 SQLite 数据库,这可能是一个漫长的过程,对 UI 不“友好”。
我不想使用“java 代码”。
TIA
【问题讨论】:
标签: c# android xamarin.android splash-screen
如何在程序启动时实现一个简单的“启动画面”?我正在复制一个 SQLite 数据库,这可能是一个漫长的过程,对 UI 不“友好”。
我不想使用“java 代码”。
TIA
【问题讨论】:
标签: c# android xamarin.android splash-screen
我最近通过以下方式解决了这个问题。
在主 Activity 中,我通过 Intent 传递了一个参数来设置启动画面保持可见的毫秒数。
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Intent i=new Intent();
i.SetClass(this, typeof (Splash));
i.PutExtra("Milliseconds", 3000);
StartActivity(i);
}
然后,在我命名为“Splash”的第二个活动中,我检索了该值并设置了第二个线程以在时间过去后结束该活动。
[Activity(Label = "Daraize Tech")]
public class Splash : Activity
{
private int _milliseconds;
private DateTime _dt;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_milliseconds = Intent.GetIntExtra("Milliseconds", 1000);
SetContentView(Resource.Layout.Splash);
_dt=DateTime.Now.AddMilliseconds(_milliseconds);
}
public override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
new Thread(new ThreadStart(() =>
{
while (DateTime.Now < _dt)
Thread.Sleep(10);
RunOnUiThread( Finish );
}
)).Start();
}
}
【讨论】:
另见http://docs.xamarin.com/android/tutorials/Creating_a_Splash_Screen 真的很棒的教程。
只需要大约 10 行代码:)
在 Styles.xml 中:
<resources>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
在你的活动中:
[Activity (MainLauncher = true, Theme = "@style/Theme.Splash", NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your async task here...
StartActivity (typeof (Activity1));
}
}
【讨论】:
ScreenOrientation = ScreenOrientation.Landscape(我的主要活动也有它),但这导致没有启动屏幕显示。如果我只是使用人像飞溅,切换到主Activity时它会旋转出来,看起来有点奇怪。
这对我有用:
从启动活动开始一个新线程。您可以等待几秒钟或加载一些数据或其他内容。
[Activity(MainLauncher = true, NoHistory = true)]
public class Splashscreen : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.splashscreen);
new Thread (new ThreadStart (() =>
{
//Load something here ...
Thread.Sleep(1500);
Intent main = new Intent (this, typeof(MainActivity));
this.StartActivity (main);
this.Finish ();
})).Start ();
}
}
【讨论】:
此解决方案可为您提供以下内容:
在 OnCreate 中,调用 SetContentView 启动闪屏,然后启动工作线程,运行缓慢处理数据初始化的东西。
这样,spalsh 屏幕就可以毫无延迟地显示出来。工作线程中的最后一条语句启动“主”应用程序/活动,它的数据库和数据都准备好供访问。从 OnCreate 调用 StartActivity()(即在 initializeDataWorker.Start() 之后),将导致 MainActivity 在创建 DB 和/或获取数据之前/期间运行,这通常是不可取的。
此解决方案缺少从后台删除启动画面的方法。当我开始实现这个功能时,我会更新它。
namespace Mono.Droid
{
[Activity(
Label = "Splash Activity",
MainLauncher = true,
Theme = "@android:style/Theme.Black.NoTitleBar",
Icon = "@drawable/icon",
NoHistory = false)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.SplashLayout);
Thread initializeDataWorker = new Thread(new ThreadStart(InitializeData));
initializeDataWorker.Start();
}
private void InitializeData()
{
// create a DB
// get some data from web-service
// ...
StartActivity(typeof(MainActivity));
}
}
}
【讨论】: