android hander 线程用法
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.frg_listtwd, null);// 显示界面布局 myListView = (ListView) view.findViewById(R.id.lsttwds);// 列表布局 loadMoreView = inflater.inflate(R.layout.load_more, null);// 加载分页 btnLoadMore = (Button) loadMoreView.findViewById(R.id.loadMoreButton);// 分页按钮 bundle = this.getArguments(); // 获取条件 conditionStr = bundle.getString("conditionStr"); ctx = view.getContext(); try { loadDataHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 0x65533) { myListView.setAdapter(myListViewAdapter); loadMoreInit(conditionStr); } if (msg.what == 0x65534) { Toast.makeText(getActivity(), "没有该患者记录:", Toast.LENGTH_LONG).show(); } } }; //根据体温单条件获取体温单数据 loadDataThread = new Thread(new Runnable() { @Override public void run() { try { TWDatasize = getTWDCount(conditionStr);// 加载初始数据 if (TWDatasize > 0) { int count = (myListViewAdapter == null) ? 0 : myListViewAdapter.getCount();// 当前已加载的条数 String result = ""; RequireParams paraminfo = new RequireParams(); paraminfo.setMethodName("TWD_QueryRequest"); WebServiceInvoker invokerinfo = new WebServiceInvoker(); invokerinfo.setParams(paraminfo); // 设置参数 SoapObject requestinfo = invokerinfo .getRequestInstance(); requestinfo.addProperty("condition", conditionStr); requestinfo.addProperty("recBegin", currentIndex + 1); if (count + pageSize <= TWDatasize) { requestinfo.addProperty("recEnd", count + pageSize); } else { requestinfo.addProperty("recEnd", TWDatasize); } result = invokerinfo.invokeWS(requestinfo) .getProperty(0).toString(); if (result.indexOf("error") >= 0) { return; } else { myTWInfos = TWInfo.getTWInfo(result, "UTF-8"); myListViewAdapter = new ListTWAdapter( TWDFragment.this.getActivity(), myTWInfos, R.layout.nurselist_items, new String[] { "jlid", "rqsj", "cshour", "JLR", "tw", "mbxl", "xy1", "xy2" }, new int[] { R.id.cbox, R.id.itemsTitle, R.id.itemsdaterecord, R.id.itemsText, R.id.nursedetail, R.id.itemsrow }); Message msg = loadDataHandler.obtainMessage(); msg.what = 0x65533; msg.sendToTarget(); } } else { Message msg = loadDataHandler.obtainMessage(); msg.what = 0x65534; msg.sendToTarget(); } } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); Toast.makeText(getActivity(), "错误:" + Log.getStackTraceString(e), Toast.LENGTH_LONG).show(); } } }); loadDataThread.start(); // 开启线程 } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); } return view; }
@Override public void onClick(View v) { lsh = "576"; startdate = setstart_date.getText().toString(); enddate = setend_date.getText().toString(); btnLoginContext = v.getContext(); HandlerThread handlerThread = new HandlerThread("handlerThread"); handlerThread.start();// 启新线程 MyHandler myHandler = new MyHandler(handlerThread.getLooper());// 建消息队列的处理器(新线程) Message msg = myHandler.obtainMessage();// 绑定自定义的处理器 Bundle bundle = new Bundle(); bundle.putString("startdate", startdate); bundle.putString("enddate", enddate); bundle.putString("lsh", lsh); msg.setData(bundle); msg.sendToTarget(); } @Override public void handleMessage(Message msg) { try { super.handleMessage(msg); Bundle bundle = msg.getData(); if (bundle == null) return; String result = ""; StringBuilder str = new StringBuilder(); str.append("<condition><ksrqsj>" + bundle.get("startdate").toString()+ "</ksrqsj>"); str.append("<jsrqsj>" + bundle.get("enddate").toString()+ "</jsrqsj>"); str.append("<lsh>" + bundle.get("lsh").toString()+ "</lsh></condition>"); RequireParams params = new RequireParams(); params.setMethodName("GetNurseTableinfoPag"); WebServiceInvoker invoker = new WebServiceInvoker(); invoker.setParams(params); //设置参数 SoapObject request = invoker.getRequestInstance(); request.addProperty("condition", str.toString()); request.addProperty("recBegin", 0); request.addProperty("recEnd", 10); result = invoker.invokeWS(request).getProperty(0).toString(); if (result.indexOf("error") >= 0) { showMyDialog(btnLoginContext, result); return; } else { List<Map<String, Object>> myNurseInfos = null; myNurseInfos = NurseInfo.getNurseInfo(result, "UTF-8"); //查询成功 Intent intent = new Intent(); Bundle mybundle = new Bundle(); final SerializableMaplist myMap=new SerializableMaplist(); myMap.setMap(myNurseInfos); mybundle.putSerializable("listdata", myMap); intent.putExtras(mybundle); intent.setClass(getActivity(), NursingPageQuery.class); startActivity(intent); } } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); Toast.makeText(getActivity(),"错误:"+ Log.getStackTraceString(e),Toast.LENGTH_LONG).show(); }