2012CSDN博客之星火热出炉!     2013年全国百所高校巡讲讲师招募

Android笔记:实现加载的progressDialog之后跳转到程序主界面       

        分类:            Android1982人阅读评论(5)收藏举报

因为考虑到有些程序加载时间相对较长,所以在启动主界面之前,实现一个加载的progressDialog对话框来延缓,相对来说可以提高用户体验度。

如果是直接进入主界面,即activity,界面因为加载时间长,一片漆黑,让用户老觉得这程序不行。

贴出源码:

方法一:

  1. public class LoadingActivity extends Activity { 
  2.     private ProgressDialog dialog; 
  3.     @Override 
  4.     protected void onCreate(Bundle savedInstanceState) { 
  5.             // TODO Auto-generated method stub 
  6.             super.onCreate(savedInstanceState); 
  7.             //设置一个progressdialog的弹窗 
  8.             dialog = ProgressDialog.show(this, null, "程序正在加载,请稍候...", true, false); 
  9.             //启动一个处理loading业务的线程 
  10.             new LoadingThread(this).start(); 
  11.     } 
  12.  
  13.     private class LoadingThread extends Thread { 
  14.             private LoadingActivity activity; 
  15.  
  16.             public LoadingThread(LoadingActivity act) { 
  17.                     Log.d(">>>>>LoadingThread", "构造方法"); 
  18.                     activity = act
  19.             } 
  20.  
  21.             public void run() { 
  22.                     Log.d(">>>>>LoadingThread", "传递message"); 
  23.                     //********************* 
  24.                     //  处理业务 
  25.                     //********************* 
  26.                      
  27.                     //发送消息 
  28.                     activity.mHandler.sendEmptyMessage(0); 
  29.             } 
  30.     } 
  31.      
  32.     //处理跳转到主Activity 
  33.     private Handler mHandler = new Handler() { 
  34.             @Override 
  35.             public void handleMessage(Message msg) { 
  36.                     // TODO Auto-generated method stub 
  37.                     Log.d(">>>>>Mhandler", "开始handleMessage"); 
  38.                     Intent mIntent = new Intent(); 
  39.                     mIntent.setClass(LoadingActivity.this, PalmHospital_main_Activity.class); 
  40.                     startActivity(mIntent); 
  41.                     LoadingActivity.this.finish(); 
  42.                     Log.d(">>>>>Mhandler", "LoadActivity关闭"); 
  43.                     if (dialog.isShowing()) 
  44.                             dialog.dismiss(); 
  45.             } 
  46.     }; 
public class LoadingActivity extends Activity {
    private ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            //设置一个progressdialog的弹窗
            dialog = ProgressDialog.show(this, null, "程序正在加载,请稍候...", true, false);
            //启动一个处理loading业务的线程
            new LoadingThread(this).start();
    }

    private class LoadingThread extends Thread {
            private LoadingActivity activity;

            public LoadingThread(LoadingActivity act) {
                    Log.d(">>>>>LoadingThread", "构造方法");
                    activity = act;
            }

            public void run() {
                    Log.d(">>>>>LoadingThread", "传递message");
                	//*********************
                    //	处理业务
                	//*********************
                    
                    //发送消息
                    activity.mHandler.sendEmptyMessage(0);
            }
    }
    
    //处理跳转到主Activity
    private Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                    // TODO Auto-generated method stub
                    Log.d(">>>>>Mhandler", "开始handleMessage");
                    Intent mIntent = new Intent();
                    mIntent.setClass(LoadingActivity.this, PalmHospital_main_Activity.class);
                    startActivity(mIntent);
                    LoadingActivity.this.finish();
                    Log.d(">>>>>Mhandler", "LoadActivity关闭");
                    if (dialog.isShowing())
                            dialog.dismiss();
            }
    };
}

方法二:相对来说要容易理解一点,贴图

  1. public class LoadingActivity extends Activity { 
  2.     private ProgressDialog dialog; 
  3.     @Override 
  4.     protected void onCreate(Bundle savedInstanceState) { 
  5.             // TODO Auto-generated method stub 
  6.             super.onCreate(savedInstanceState); 
  7.             //设置一个progressdialog的弹窗 
  8.             dialog = ProgressDialog.show(this, "加載中...", "正在搜尋。。。。,請稍後!"); 
  9.             Thread thread = new Thread(new Runnable() { 
  10.                     public void run() { 
  11.                                                                  //do... 
  12.                                                              
  13.                             Message message = new Message(); 
  14.                             message.what = 0
  15.                             mHandler.sendMessage(message); 
  16.                     } 
  17.             }); 
  18.             thread.start(); 
  19.  
  20.     } 
  21.      
  22.     //处理跳转到主Activity 
  23.     private Handler mHandler = new Handler() { 
  24.             @Override 
  25.             public void handleMessage(Message msg) { 
  26.                     // TODO Auto-generated method stub 
  27.                     Log.d(">>>>>Mhandler", "开始handleMessage"); 
  28.                     Intent mIntent = new Intent(); 
  29.                     mIntent.setClass(LoadingActivity.this, PalmHospital_main_Activity.class); 
  30.                     startActivity(mIntent); 
  31.                     LoadingActivity.this.finish(); 
  32.                     Log.d(">>>>>Mhandler", "LoadActivity关闭"); 
  33.                     if (msg.what == 0) { 
  34.                         dialog.dismiss(); 
  35.                 } 
  36.             } 
  37.     }; 
posted on 2013-01-17 15:00  zhengbeibei  阅读(10676)  评论(0编辑  收藏  举报