启动画面无法在应用中加载

时间:2012-11-05 09:53:42

标签: android splash-screen

我已经为Android应用中的加载页面添加了启动画面的代码,但没有任何改变,任何人都可以解决这个问题吗?

这是我的代码:

Thread splashThread = new Thread() {
        @Override
        public void run() {
           try {
              int waited = 0;
              while (waited < 1000) {
                 sleep(100);
                 waited += 100;
              }
           } catch (InterruptedException e) {
              // do nothing
           } finally {

              Intent i = new Intent(Splash.this,Activity1.class);

              startActivity(i);
              finish();
           }
        }
     };
     splashThread.start();
    }

3 个答案:

答案 0 :(得分:1)

使用runOnUiThread从单独的Thread启动活动:

 Splash.this.runOnUiThread(new Runnable() {
                public void run() {
                  // some code #3 (that needs to be ran in UI thread)
                  Intent i = new Intent(Splash.this,Activity1.class);

                 startActivity(i);
                 finish();
                }
            });

答案 1 :(得分:1)

使用以下代码:

Handler handler = new Handler();

        // run a thread after 2 seconds to start the home screen
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {

                // make sure we close the splash screen so the user won't come back when it presses back key

                finish();
                // start the home screen

                Intent intent = new Intent(SplashScreen.this, Home.class);
                SplashScreen.this.startActivity(intent);

            }

        }, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called

    }

答案 2 :(得分:0)

通常,当您创建初始屏幕时,它是一个完全成熟的Activity,负责显示一些用户界面。

你应该做的是创建一个扩展Activity的新类,并在其onCreate方法中运行你的con。

在此之后,您应该更改清单文件以将活动定义为启动器,如下所示:

    <activity
        android:name=".SplashActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>