首页 > 代码库 > 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 };
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 };
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 }
Android 多线程小结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。