线程的小应用

来源:互联网 发布:开淘宝店怎么注册视频 编辑:程序博客网 时间:2024/05/18 00:39
   接背景及起始宣传界面,(本次任务)然后等待三秒钟,打开MAINACTIVITY,结束SPLASH。

          //wait 3 seconds

          //start MainActivity

          //finish splash



     在protected void onCreate(Bundle savedInstanceState){

                 super.onCreate(savedInstanceState);                                    

                 setContentView(R.layout.splash);

          }

     中定义一个线程,Thread timer = new Theard(){

                                              public void run(){

                                                      try{

                                                            sleep(3000);                                                  // time是一个长整型,以毫秒定义的 (wait 3 seconds)

                                                      }catch(InterruptedException e){                       // 为防止不正常启动,增加catch

                                                             e.printStackTrace();

                                                      }finally{                                                                  // 等待三秒钟,如果没有出现异常,就在finally中启动MainActivity。

                                                             Intent openMainActivity = new Intent();     // startActivity()中需要Intent,所以在此定义一个Intent。(start MainActivity)

                                                             startActivity(openMainActivity);

                                                      }

                                               }

                                       };

      在AndroidManifest.xml的MAINACTIVITY activity中<inter-filter><action android:name="android.intent.action.MAINACTIVITY"/>改为<action android:name="com.gec_lab(picturename).appname.MAINACTIVITY"/>。





       protected void onCreate(Bundle savedInstanceState){

               super.onCreate(savedInstanceState);                                    

               setContentView(R.layout.splash);

       

               Thread timer = new Theard(){

                        public void run(){

                                try{

                                       sleep(3000);                               

                                 }catch(InterruptedException e){               

                                       e.printStackTrace();                  // 将异常简单输出

                                 }finally{                                       

                                        Intent openMainActivity = new Intent();    

                                        startActivity(openMainActivity);                   // 启动MainActivity

                                 }

                        }

                };

                timer.start();                 // 启动线程

        }

        protected void onPause(){                                                // 重写onPause,单击右键,Source->Override/Implement Methods->onPause()

                super.onPause();

                finish();                                                                              // finish splash

       }

0 0
原创粉丝点击