启动Splash屏幕活动

时间:2013-08-21 11:58:28

标签: android splash-screen

我想编写一个使用启动画面的代码。到目前为止我已经写过这个了,但有谁能告诉我这里缺少什么!?

这是我的主要代码:

package com.example.splash;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

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


    }



} 

这是我的启动活动代码:

package com.example.splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class splashscreen extends Activity {

protected int _splashTime = 5000; 

    private Thread splashTread;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splashh);
             final splashscreen sPlashScreen = this; 

                splashTread = new Thread() {
                    @Override
                    public void run() {
                        try {                   
                            synchronized(this){
                                wait(_splashTime);
                            }

                        } catch(InterruptedException e) {} 
                        finally {
                            finish();

                            Intent i = new Intent();
                            i.setClass(sPlashScreen,MainActivity.class);
                            startActivity(i);

                            //stop();
                        }
                    }
                };


                splashTread.start();

    }

问题是我不知道怎么告诉我的主要去飞溅活动,如果我使用意图我会坚持无限循环。

7 个答案:

答案 0 :(得分:3)

你可以简单地使用它:

 Handler handler=new Handler();
        handler.postDelayed(new Runnable()
        {               
            @Override
            public void run() 
            {
                Intent intent = new Intent(SplashViewController.this,HomeViewController.class);
                startActivity(intent);
                SplashViewController.this.finish();                         
            }
        }, 3000);

答案 1 :(得分:1)

试试这个:

public class splashscreen extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_splash);
            Thread t = new Thread(Splash_Runnable);
            t.start();

        }





        Runnable Splash_Runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(3000);

                        startActivity(new Intent(splashscreen.this,
                                MainActivity.class));
                        splashscreen.this.finish();

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        };

    }

答案 2 :(得分:1)

问题(我猜)是您的应用以MainActivity作为启动器活动开始。在您的Application Manifest XML中创建splashscreen laucher Activity,您将避免无限循环。

答案 3 :(得分:1)

试试此代码:

private boolean _active = true;
private int _splashTime = 5000;

Thread splashTread = new Thread() 
        {
            @Override
            public void run() 
            {
                try 
                {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) 
                    {
                        sleep(100);
                        if(_active) 
                        {
                            waited += 100;
                        }
                    }
                } 
                catch(InterruptedException e) 
                {
                    e.printStackTrace();
                } 
                finally 
                {
                        Intent intent = new Intent(SplashScreenActivity.this,MainActivity.class);
                        startActivity(intent);
                    finish();
            }
        };
        splashTread.start();
{p}在AndroidManifest中提及您的活动为主要活动。

答案 4 :(得分:1)

尝试从此处更改您的SplashActivity代码。

Splash and main activity error

同时将您的splashactivtiy作为启动器活动,然后从SplashScreen重定向到MainActivity

<activity
            android:name="com.app.wablogic.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

答案 5 :(得分:0)

创建启动活动的详细信息

启动

创建布局

splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/splash" >


</LinearLayout>

现在在包下创建一个类。将其命名为启动

public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);
            startActivity(openMainActivity);
            finish();

        }
    }, 5000);  //it will call the MainActivity after 5 seconds  
}

转到manifest并将Activity添加到其中。

并剪切主要和启动器为子节点的intent-filter并将其粘贴到启动活动中

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

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

答案 6 :(得分:0)

你可以创建一个线程来做某事或只是睡几秒钟来做,比如

公共类MainActivity扩展了Activity {

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

    Thread background = new Thread() {
        public void run() {

            try {
                // Thread will sleep for 3 seconds
                sleep(3*1000);

                // After 3 seconds redirect to another intent
                Intent i=new Intent(getBaseContext(),MenuActivity.class);
                startActivity(i);

                //Remove activity
                finish();

            } catch (Exception e) {

            }
        }
    };

    background.start();

}

@Override
protected void onDestroy() {

    super.onDestroy();

}

您可以获得更多示例here