开发者

MonoDroid Splash Screen

开发者 https://www.devze.com 2023-03-16 19:04 出处:网络
How can I implement a 开发者_C百科simple \"splash screen\" on program startup?I am copying a SQLite DB and it can be a bit of a long process that is not UI \"friendly\".

How can I implement a 开发者_C百科simple "splash screen" on program startup? I am copying a SQLite DB and it can be a bit of a long process that is not UI "friendly" .

I would prefer not to use "java code".

TIA


I recently solved this problem in the following way.

In the main activity I passed a parameter via the intent to set the number of milliseconds for which the splash screen would remain visible.

    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);
    }

Then, in the second activity which I named "Splash" I retrieved the value and set a second thread to end the activity when the time had elapsed.

[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();
    }

}


Also see http://docs.xamarin.com/android/tutorials/Creating_a_Splash_Screen Really great tutorial.

It only takes about 10 lines of code :)

In a 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>

In your activity:

[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));
    }
}


This worked for me:

Starting an new thread from the splash activity. You can wait a few seconds or load some data or something else.

[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 ();
    }
}


This solution gets you the following:

  • Immediate display of the splash screen
  • Removal of splash screen the exact time the "main" activity is launched (main activity replaces the splash activity)

In OnCreate, SetContentView is called to get the splash screen up, and then the worker thread is kicked off, which runs the slow processing data initialization stuff.

This way, the spalsh screen is displayed without delay. The last statement in the worker thread kicks off the "main" app/activity, which will have its DB & data all ready for access. Calling StartActivity() from OnCreate (ie, after initializeDataWorker.Start()), will cause MainActivity to run before/while the DB is being created and/or data is being fetched, which is usually not desireable).

This solution lacks a way to remove the splash screen from the backstack. When I get around to implementing this functionality I'll update it.

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));
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消