自定义开机画面,以在Android中加载变量 [英] Custom Splash Screen to load variables in Android

查看:147
本文介绍了自定义开机画面,以在Android中加载变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始研究这个: Android的闪屏 和我的应用程序完美的作品用这种方法。 不幸的是,我已经开发了一个自定义开机画面(不是一个进步像在其他对话框后),我不能够使用同样的方法。我的启动画面是,我开始从的onCreate调用它作为一个不同的线程不同的活动。

I start studying this: Android SplashScreen and my app works perfectly with this method. Unfortunately, I've developed a custom splash screen (not a progress dialog like in the other post) and I'm not able to use the same approach. My splashscreen is a different activity that I starts calling it from the onCreate as a different thread.

而不是 this.pd = ProgressDialog.show的(这一点,工作......,下载数据......,真,假);在OnCreate 我做的:

        Thread splashThread = new Thread() {
                @Override
                public void run() {
                     // start the splash screen activity
                }
        };
        splashThread.start();
 }

在splashscreeen活动正常启动。 所以,我所说的AsyncTask的作为默认的方法(doInBackground,onPostExecute),我不能停止运行的启动画面的活动,回来的主要原因之一,在所有的变量都在doInBackground加载。

The splashscreeen activity correctly starts. So I call the AsyncTask as in the default method (doInBackground, onPostExecute) and I'm not able to end the activity that is running the splashscreen and come back to the main one, after all the variables are loaded in the doInBackground.

任何建议?

推荐答案

好了,我终于感谢JPM的使用处理程序的建议,我解决了是:

Well, I finally thank JPM for the suggestion to use handlers and I solved with that:

public class SplashScreen extends Activity {

        private static Handler mHandler ;
        private static Handler mainHandler ;

        protected static final int CLOSE_SPLASH = 0 ;

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

        mHandler = new Handler(){
                @Override
                public void handleMessage(Message msg){
                        switch(msg.what){
                        case CLOSE_SPLASH:
                        finish();
                        break;
                        }

                }
        };
    }

    @Override
    public void onStart(){
        super.onStart();
        if(mainHandler != null){
                mainHandler.sendEmptyMessage(MainActivity.START_LOAD);
        }
    }

    @Override
    public boolean onKeyDown (int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK){
                mainHandler.sendEmptyMessage(MainActivity.ABORT_LOAD);
        }
        return super.onKeyDown(keyCode, event) ;
    }
    public static void setMainHandler(Handler h){
        mainHandler = h ;
    }
    public static void sendMessage(Message msg){
        mHandler.sendMessage(msg);
    }
    public static void sendMessage(int w){
        mHandler.sendEmptyMessage(w);
    }
}

在MainActivity我管理处理器来回:

In the MainActivity I manage handlers back and forth:

public class MainActivity extends Activity {

        private MainActivity _this;
        private Handler mHandler;

        protected static final int FINISH_LOAD = 0 ;
        protected static final int START_LOAD = 1 ;
        protected static final int ABORT_LOAD = 2 ;

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

                _this = this;

                mHandler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                                switch (msg.what) {
                                case FINISH_LOAD:
                                   SplashScreen.sendMessage(SplashScreen.CLOSE_SPLASH);
                                        break;
                                case START_LOAD:
                                        initializing();
                                        break;
                                case ABORT_LOAD:
                                        finish();
                                }

                        }
                };
                startSplash();
        }

        private void startSplash() {
                Intent intent = new Intent(this, SplashScreen.class);
                SplashScreen.setMainHandler(mHandler);
                startActivity(intent);
        }

        private void initializing() {
                new Thread() {
                        @Override
                        public void run() {    
                                long start_time = android.os.SystemClock.uptimeMillis();
                                doTheHeavyJob();
                                long duration = android.os.SystemClock.uptimeMillis() - start_time;
                                if (duration <=3000) {
                                   try {
                                       wait(3000-duration);
                       } catch (InterruptedException e) {
                          e.printStackTrace();
                    }
                        }
                        mHandler.sendEmptyMessage(FINISH_LOAD);
                  }
            }.start();
        }    
}

在这种方式我可以管理doTheHeavyJob()函数,并在这两种情况下完成的闪屏:在作业完成后至少经过3000米利斯,以显示我的闪屏的最低期限。 我也想罐Teskio在anddev意大利网站最在这里完成的工作。

In this manner I can manage the doTheHeavyJob() function and finish the SplashScreen in both cases: after the job finish and at least after 3000 millis, the minimum duration of my splashscreen to be shown. I also want to tank Teskio on the anddev italian website for the most of the job done here.

这篇关于自定义开机画面,以在Android中加载变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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