飞溅屏幕不会消失

时间:2013-11-09 19:57:10

标签: java android multithreading screen splash

我正在尝试创建一个在我的应用程序的其余部分加载时出现的启动画面的线程,但由于某种原因,我的启动活动在2秒后不会消失。那是为什么?

这是我的Splash活动类:

imports ...

public class Splash extends Activity implements Runnable {

    @Override
    protected void onCreate(Bundle tokenArg) {
        super.onCreate(tokenArg);
        setContentView(R.layout.splash);

        Thread splashing = new Thread();
        splashing.start();

    }

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            startActivity(new Intent(Splash.this, Home.class));
        }
        catch(Exception excpt) {
            AlertDialog alert = new AlertDialog.Builder(this).create();
            alert.setTitle("Error");
            alert.setMessage("App is going to close");
        }
        finally {
            this.finish();
        }
    }
}

这是.Home活动类:

public class Home extends Activity {

    @Override
    protected void onCreate(Bundle tokenArg) {
        super.onCreate(tokenArg);
        setContentView(R.layout.activity_home);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }
}

两者都有相应的xml,它们都很好用。 (我已经单独测试过了)

提前感谢您的时间。

5 个答案:

答案 0 :(得分:2)

在实例化线程时,您没有将runnable作为参数传递给Thread构造函数。在Splash.class中实现接口Runnable后,将当前对象作为参数传递。

Thread splashing = new Thread(this);
splashing.start();

希望这会有所帮助。

答案 1 :(得分:1)

而不是线程使用此代码来启动您的活动。

new Handler().postDelayed(new Runnable(){
                public void run() {
                    Intent mainIntent = new Intent(Splash.this, Home.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    Splash.this.startActivity(mainIntent);

                    Splash.this.finish();
                }
            }, 2000); 

答案 2 :(得分:0)

试试这段代码。它处理了大量的用例,可以很好地处理用户的后退按钮和许多其他用例。

public class SplashBranding extends Activity {

    private Handler mHandler;
    private static final long TWO_SECOND_IN_MS = 2000; 

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mHandler = new ShowHomeHandler();   
        setContentView(R.layout.splash);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.sendEmptyMessageDelayed(0, TWO_SECOND_IN_MS);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // In case system dialogs appear and this method is called, we shouldn't show Home. 
        if(!isFinishing())
            mHandler.removeMessages(0);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // In case a call is received and this method is called, we shouldn't show Home.
        if(!isFinishing())
            mHandler.removeMessages(0);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // In case user pressed back, we shouldn't show Home.
        mHandler.removeMessages(0);
    }

    /**
     * Shows the Home Activity.
     */
    private void showHome() {
        Intent i = new Intent(this, Home.class);
        startActivity(i);
        finish();
    }

    private class ShowHomeHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
            case 0:
                showHome();
                break;
            default:
                break;
            }
        }
    }

}

答案 3 :(得分:0)

看起来你没有传递new Thread();的课程。您应该尝试将其更改为new Thread(this);

答案 4 :(得分:0)

您可以尝试这个简单的代码 ..

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    *// activity_splay is the name of splash .xml file*
     setContentView(R.layout.activity_splash);
    Thread thread = new Thread(){
        @Override
        public void run(){
            try{
                // splash screen would disappear after 1500 ms i.e 1.5 sec
                sleep(1500); <br/>
         // MainActivity is the name of activity that would appear after splash screen 
         startActivity(new Intent(getApplicationContext(), MainActivity.class));
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    };
    thread.start();
}
//hopefully it would help:)