首页 > 代码库 > Android 多线程小结

Android 多线程小结

1、启动欢迎界面

 1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3     // TODO Auto-generated method stub 4     super.onCreate(savedInstanceState); 5     setContentView(R.layout.welcome); 6     new Handler().postDelayed(r, 1000);// 1秒后关闭,并跳转到主页面 7 } 8  9 Runnable r = new Runnable() {10 11     @Override12     public void run() {13     // TODO Auto-generated method stub14     Intent intent = new Intent();15     intent.setClass(WelcomeActivity.this, MainActivity.class);16     startActivity(intent);17     finish();18     }19 };
View Code

2、远程数据库查询

 1 // 主线程启动数据库查询线程 2         Thread t = new Thread(QueryDB); 3         t.start(); 4         // 等待子线程结束 5         try { 6             ((Thread) t).join(); 7         } catch (InterruptedException e) { 8             e.printStackTrace(); 9         }10         listview.setAdapter(adapter);11         listview.setVisibility(View.VISIBLE);12         listview.setItemsCanFocus(false);13 14 15     // 数据库查询子线程,共享数据16     Runnable QueryDB = new Runnable()17     { 18         @Override19         public void run()20         {21             // 此处写访问  webservice 代码22             List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();23             list = dbUtil.getAllInfo();24             // ListItem25             adapter = new SimpleAdapter(26                     SpeLineListActivity.this, 27                     list, 28                     R.layout.v1_speline_item, 29                     new String[] { "Cno", "Cname", "Cnum" }, 30                     new int[] { R.id.txt_company_name, R.id.txt_sl_line, R.id.txt_sl_contact }31                     );32 33         }34     };
View Code

3、多线程查询

1 List<Thread> tList = new ArrayList<Thread>();2 for (int i = 0; i < tList.size(); i++) {3 try {4     tList.get(i).join();5     } catch (InterruptedException e) {6     e.printStackTrace();7     }8 }
View Code

 

Android 多线程小结