C# 单机器人启动屏幕
如何在程序启动时实现简单的“启动屏幕”?我正在复制一个SQLite数据库,这可能是一个有点长的过程,对UI不友好 我宁愿不使用“java代码”C# 单机器人启动屏幕,c#,android,xamarin.android,C#,Android,Xamarin.android,如何在程序启动时实现简单的“启动屏幕”?我正在复制一个SQLite数据库,这可能是一个有点长的过程,对UI不友好 我宁愿不使用“java代码” TIA我最近用以下方法解决了这个问题 在主活动中,我通过intent传递了一个参数,以设置启动屏幕保持可见的毫秒数 protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from t
TIA我最近用以下方法解决了这个问题 在主活动中,我通过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();
}
}
[活动(Label=“Daraize Tech”)]
公共课:活动
{
私有整数毫秒;
私有日期时间;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
_毫秒=Intent.GetIntExtra(“毫秒”,1000);
SetContentView(Resource.Layout.Splash);
_dt=DateTime.Now.add毫秒(_毫秒);
}
public override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
新线程(新线程开始(()=>
{
while(DateTime.Now<\u dt)
睡眠(10);
RunOnUiThread(完成);
}
)).Start();
}
}
另请参见
非常棒的教程
它只需要大约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>
此解决方案可为您提供以下功能:
- 立即显示启动屏幕
- 启动“主”活动的确切时间(主活动取代启动活动)后,删除启动屏幕
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));
}
}
}
这对我很有用:
从splash活动启动新线程。您可以等待几秒钟或加载一些数据或其他内容
[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 ();
}
}
鲍勃,谢谢你,这很有效。我需要添加一个“使用System.Threading”。在我看来,没有更好的方法来解决这个问题。谢谢我很难将其用于横向启动屏幕,我在“启动活动”属性中设置了
ScreenOrientation=ScreenOrientation.scape
(我的主要活动也有该属性),但这导致没有显示任何启动屏幕。如果我只是使用一个肖像飞溅,它会在切换到主要活动时旋转出来,这看起来有点奇怪。3年多了,你还没有来得及从后台执行删除?;)@德鲁伊一世可能会在不久的将来重游安罗伊德。如果我这样做的话,我一定会抽出时间去做一些后盾移除!
[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 ();
}
}