Android:不显示启动画面,为什么?

发布于 2024-11-14 19:19:04 字数 710 浏览 1 评论 0原文

我正在努力学习Android。从到目前为止我读过的文档中,我无法弄清楚如何显示启动视图(在睡眠期间屏幕保持空白)。看来我需要为主布局启动一个新的活动,但这似乎很浪费(飞溅应该永远消失,我想重用它的线程)。

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Ext3 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Log.v("Ext3", "starting to sleep");

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

        Log.v("Ext3", "done sleeping");

        setContentView (R.layout.main);
    }

}

I am working on learning Android. From the documents I have read so far I can't figure out how to get the splash View to show (during the sleep the screen stays blank). It appears I need to start a new activity for the main layout, but this seems wasteful (the splash should be forever gone, I'd like to reuse its thread).

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Ext3 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Log.v("Ext3", "starting to sleep");

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

        Log.v("Ext3", "done sleeping");

        setContentView (R.layout.main);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲影 2024-11-21 19:19:04

我相信你的启动屏幕永远不会显示,因为你永远不会给 UI 线程(你所在的)绘制它的机会,因为你只是睡在那里什么也不做。

我建议您查看 Timer 或类似的东西,而不是 Thread.sleep安排刷新和更改视图内容;另一种方法是启动一个 AsyncTask ,您可以在更改视图之前先在其中休眠现在。

不要睡眠或以任何其他方式阻塞 UI 线程,这很糟糕......(导致 ANR)

I believe your splash screen never gets shown because you never give the UI thread (that you are in) a chance to draw it since you just sleep there doing nothing.

Instead of the Thread.sleep, I would suggest you look into a Timer or something like that to schedule the refresh and changing the content of your view; an alternative would be to start an AsyncTask where you could sleep before changing the view as you are doing now.

Do not sleep or in any other way block the UI thread, that's bad... (causes ANR)

谁与争疯 2024-11-21 19:19:04

当您像这样调用 sleep 时,就会阻塞 UI 线程。相反,将对 setContentView 的第二次调用放在 Runnable 中,创建一个处理程序,并使用处理程序的 postDelayed 方法来运行 Runnable。像这样的东西:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Runnable endSplash = new Runnable() {
        @Override
        public void run() {
            setContentView (R.layout.main);
        }
    }
    new Handler().postDelayed(endSplash, 5000L);
}

When you call sleep like that, you are blocking the UI thread. Instead, put the second call to setContentView in a Runnable, create a Handler, and use the Handler's postDelayed method to run the Runnable. Something like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Runnable endSplash = new Runnable() {
        @Override
        public void run() {
            setContentView (R.layout.main);
        }
    }
    new Handler().postDelayed(endSplash, 5000L);
}
夏天碎花小短裙 2024-11-21 19:19:04

我已经在我的应用程序中尝试过这段代码,它工作得很好。也许它会对您有所帮助。

public class SplashScreen extends Activity {

    /**
     * The thread to process splash screen events
     */
    private Thread mSplashThread;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Splash screen view
        setContentView(R.layout.splash);



        final SplashScreen sPlashScreen = this;

        // The thread to wait for splash screen events
        mSplashThread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        // Wait given period of time or exit on touch
                        wait(5000);
                    }
                } catch (InterruptedException ex) {
                }

                finish();

                // Run next activity
                Intent intent = new Intent();
                intent.setClass(sPlashScreen, MainActivity.class);
                startActivity(intent);
                stop();
            }
        };

        mSplashThread.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return false;
    }

    /**
     * Processes splash screen touch events
     */
    @Override
    public boolean onTouchEvent(MotionEvent evt) {
        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
            synchronized (mSplashThread) {
                mSplashThread.notifyAll();
            }
        }
        return true;
    }

}

I have tried this code in my application and its working perfectly.May be it will help you.

public class SplashScreen extends Activity {

    /**
     * The thread to process splash screen events
     */
    private Thread mSplashThread;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Splash screen view
        setContentView(R.layout.splash);



        final SplashScreen sPlashScreen = this;

        // The thread to wait for splash screen events
        mSplashThread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        // Wait given period of time or exit on touch
                        wait(5000);
                    }
                } catch (InterruptedException ex) {
                }

                finish();

                // Run next activity
                Intent intent = new Intent();
                intent.setClass(sPlashScreen, MainActivity.class);
                startActivity(intent);
                stop();
            }
        };

        mSplashThread.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return false;
    }

    /**
     * Processes splash screen touch events
     */
    @Override
    public boolean onTouchEvent(MotionEvent evt) {
        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
            synchronized (mSplashThread) {
                mSplashThread.notifyAll();
            }
        }
        return true;
    }

}
俯瞰星空 2024-11-21 19:19:04

这是基本启动屏幕的片段

public class Splash extends Activity {

//private ProgressDialog pd = null;
private final int SPLASH_DISPLAY_LENGTH = 3000; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splashscreen);
    //this.pd = ProgressDialog.show(this, "Initializing..", "Initializing Infraline...", true, false);

    /* New Handler to start the InfralineTabWidget-Activity
     * and close this Splash-Screen after some seconds.*/

    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
        /* Create an Intent that will start the InfralineTabWidget-Activity. */
            Intent mainIntent = new Intent(Splash.this,InfralineTabWidget.class);
            Splash.this.startActivity(mainIntent);
            Splash.this.finish();
        }
    }, SPLASH_DISPLAY_LENGTH);

}

}

并在您的 AndroidManifest.xml 中放置

    <activity android:name=".Splash" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="orientation|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

希望这对您有用:)

This is the snippet for a basic splash screen

public class Splash extends Activity {

//private ProgressDialog pd = null;
private final int SPLASH_DISPLAY_LENGTH = 3000; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splashscreen);
    //this.pd = ProgressDialog.show(this, "Initializing..", "Initializing Infraline...", true, false);

    /* New Handler to start the InfralineTabWidget-Activity
     * and close this Splash-Screen after some seconds.*/

    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
        /* Create an Intent that will start the InfralineTabWidget-Activity. */
            Intent mainIntent = new Intent(Splash.this,InfralineTabWidget.class);
            Splash.this.startActivity(mainIntent);
            Splash.this.finish();
        }
    }, SPLASH_DISPLAY_LENGTH);

}

}

And in your AndroidManifest.xml put

    <activity android:name=".Splash" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="orientation|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Hopefully this works for you :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文