有在Android应用程序中的启动画面最好的方法? [英] Best way to have a splash screen in an Android application?

查看:118
本文介绍了有在Android应用程序中的启动画面最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个启动画面了我的申请。 尝试创建有我的闪屏图像的活动;并尝试使用for循环,并引入时间延迟的Timer类。但din't工作方式。 难道我做错了;如果是,什么是正确的方式?

I need a splash screen for my application. Tried creating an activity having the image for my splash screen; and tried using for loop and the Timer class for introducing a time delay. But it din't work that way. Am I doing it wrong; if yes, what is the right way?

推荐答案

以上的解决方案是好的,但如果用户presses背键(并关闭您的应用程序)溅延迟之前结束。 该应用程序仍然可能会打开下一个活动,这是不是真的人性。

The above solutions are good, but what if the user presses the back-key (and closes your app) before the splash-delay is over. The app will probably still open the next Activity, which isn't really user-friendly.

这就是为什么我有一个自定义处理程序的工作,并在的onDestroy删除任何未决消息()。

That's why I work with a custom Handler, and remove any pending messages in onDestroy().

public class SplashActivity extends Activity
{
    private final static int MSG_CONTINUE = 1234;
    private final static long DELAY = 2000;

    @Override
    protected void onCreate(Bundle args)
    {
        super.onCreate(args);
        setContentView(R.layout.activity_splash);

        mHandler.sendEmptyMessageDelayed(MSG_CONTINUE, DELAY);
    }

    @Override
    protected void onDestroy()
    {
        mHandler.removeMessages( MSG_CONTINUE );    
        super.onDestroy();
    }

    private void _continue()
    {
        startActivity(new Intent(this, SomeOtherActivity.class));
        finish();
    }

    private final Handler mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            switch(msg.what){
                case MSG_CONTINUE:
                    _continue();
                    break;
            }
        }
    };
}

这篇关于有在Android应用程序中的启动画面最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆