1

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

这是我的主要代码:

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

    }

问题是我不知道如何告诉我的主要去启动活动,如果我使用意图我会陷入无限循环。

4

7 回答 7

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);
于 2013-08-21T12:11:21.013 回答
1

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

飞溅和主要活动错误

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

<activity
            android:name="com.app.wablogic.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
于 2013-08-21T12:10:15.820 回答
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();
                }

            }
        };

    }
于 2013-08-21T12:03:42.180 回答
1

问题(我猜)是您的应用程序从您MainActivity的启动器 Activity 开始。在您的应用程序清单 XML 中创建您的laucher splashscreenActivity,您将避免无限循环。

于 2013-08-21T12:04:03.507 回答
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();

提及AndroidManifest您的活动作为主要活动。

于 2013-08-21T12:09:33.890 回答
0

创建启动 Activity 的完整细节

为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>

现在在 package 下创建一个类。将其命名为飞溅

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-filtermain 和 Launcher 的子项并将其粘贴到Splash Activity 中,例如

 <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>
于 2014-08-05T12:51:22.167 回答
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();

}

你可以在这里得到更多的例子。

于 2014-10-23T06:11:17.807 回答