如何在程序启动时实现简单的“启动画面”?我正在复制一个SQLite数据库,它可能是一个很长的过程,不是UI“友好”。
我不想使用“java代码”。
TIA
答案 0 :(得分:4)
我最近以下列方式解决了这个问题。
在主要活动中,我通过意图传递一个参数来设置启动画面保持可见的毫秒数。
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();
}
}
答案 1 :(得分:4)
另见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));
}
}
答案 2 :(得分:1)
这对我有用:
从启动活动启动新线程。您可以等几秒钟或加载一些数据或其他内容。
[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 ();
}
}
答案 3 :(得分:0)
此解决方案为您提供以下内容:
在OnCreate中,调用SetContentView以启动启动画面,然后启动工作线程,这将运行缓慢处理数据初始化的东西。
这样,spalsh屏幕会毫不拖延地显示出来。工作线程中的最后一个语句启动了“主要”应用程序/活动,它将具有其DB&amp;所有数据都准备好访问。从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));
}
}
}