0

我已经设置了一个启动屏幕,我希望它在 5 秒后重定向到我的主要活动,但我的应用程序始终停留在启动屏幕上并且从不重定向。

我的代码中有以下代码SplashActivity.Java

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.SplashActivity);
        Thread timer = new Thread() {
            public void run() {
                try {
                    // sleep(R.integer.SplashActivityTime);
                    sleep(5000);
                } catch (InterruptedException iEx) {
                    iEx.printStackTrace();
                } finally {
                    Intent mainActivity = new Intent(
                           "com.myApp.myApp.MainActivity");

                    startActivity(mainActivity);
                }
            }
        };
        timer.start();
    }
}

Manifest我有:

<activity
    android:name="com.myApp.myApp.SplashActivity"
    android:label="@string/app_name" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name="com.myApp.myApp.MainActivity"
    android:label="@string/app_name" >

    <intent-filter>
        <action android:name="com.myApp.myApp.MainActivity" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
4

6 回答 6

0

使用以下代码

Thread logoTimer = new Thread()
    {
        @Override
        public void run()
        {
            try
            {
                sleep(3000);
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }//End of try block
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }//End of catch block
            finally
            {
                finish();
            }//End of finally block
        }//End of run method
    };//End of anonymous thread inner class
    logoTimer.start();

Manifest

<activity
    android:name="com.myApp.myApp.SplashActivity"
    android:label="@string/app_name" >

    <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
<activity
        android:name="com.myApp.myApp.MainActivity"
        android:label="@string/app_name" >
    </activity>
于 2013-10-17T10:56:33.517 回答
0

也许试试这个:

公共类闪屏扩展了 Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash); // Your Splash Layout

        Handler handler = new Handler();

     // run a thread
        handler.postDelayed(new Runnable() {

            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, TabDemo2Activity.class);
                splashscreen.this.startActivity(intent);

            }

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

}
}
于 2013-10-17T10:58:21.833 回答
0

使用此代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Thread timer = new Thread() {
        public void run() {
            try {
                // sleep(R.integer.SplashActivityTime);
                sleep(5000);
            } catch (InterruptedException iEx) {
                iEx.printStackTrace();
            } finally {
                Intent mainActivity = new Intent(MainActivity.this,
                        SecondActivity.class);

                startActivity(mainActivity);
                finish();
            }
        }
    };
    timer.start();
}

并在清单中

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.sample.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

     <activity
        android:name="com.example.sample.SecondActivity"
        android:label="@string/app_name" >

    </activity>
</application>
于 2013-10-17T11:19:35.907 回答
0

使用下面的处理程序对我有用。

处理程序处理程序 = 新处理程序();

// run a thread after 2 seconds to start the home screen

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
                if (!mIsBackButtonPressed) {
                    // start next activity
                    Intent intent = new Intent(SplashScreen.this, SEOshopMainActivity.class);
                    startActivity(intent);
               } 
            }
        }, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
于 2013-10-17T11:01:10.557 回答
0

改变你的意图:

Intent mainActivity = new Intent(this, MainActivity.class);

此外,哪个类是该sleep 方法的所有者?

于 2013-10-17T10:50:28.193 回答
0

删除清单文件中主要活动活动的意图过滤器

实现启动画面的最佳方式点击这里

并开始意图线更改为

Intent mainActivity = new Intent(SplashActivity .this, MainActivity.class);
于 2013-10-17T10:51:25.073 回答