2012CSDN博客之星火热出炉! 2013年全国百所高校巡讲讲师招募
因为考虑到有些程序加载时间相对较长,所以在启动主界面之前,实现一个加载的progressDialog对话框来延缓,相对来说可以提高用户体验度。
如果是直接进入主界面,即activity,界面因为加载时间长,一片漆黑,让用户老觉得这程序不行。
贴出源码:
方法一:
- 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();
- }
- };
- }
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(); } }; }
方法二:相对来说要容易理解一点,贴图
- 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, "加載中...", "正在搜尋。。。。,請稍後!");
- Thread thread = new Thread(new Runnable() {
- public void run() {
- //do...
- Message message = new Message();
- message.what = 0;
- mHandler.sendMessage(message);
- }
- });
- thread.start();
- }
- //处理跳转到主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 (msg.what == 0) {
- dialog.dismiss();
- }
- }
- };
- }