44289533 发表于 2013-1-30 04:02:31

android线程超时的例子 多多指教!Skype:chu_jingwei

综合以前写的线程例子 演变成现在这个样子
具体的思路 跟上一个帖子没有什么变化,只是优化了点代码! 这里就不写注释了!
http://44289533.iteye.com/admin/blogs/1714483可能代码不是很周到,但是基本可以实现超时的问题,如果大家有更好的建议 不妨留下您的建议,咱们共同讨论一下! 感谢了!

我的Skype: chu_jingwei 初京蔚

注: handler switch case 3:  这个模块是在 数据访问层 假如出现SQLException 发送的消息a


private Thread threadA;private Thread threadB;private boolean isTimeOut;private boolean isAllDone;private boolean isStopThread;isTimeOut = false;isAllDone = false;isStopThread = false;showWaitDialogNoTitle(getString(R.string.MSG_I_0004));//タイムアウトThreadthreadA = new Thread(new Runnable() {@Overridepublic void run() {Looper.prepare();try {Thread.sleep(CHECK_TIME);if (!isAllDone && !isStopThread&&!Thread.interrupted()) {isTimeOut = true;threadB.interrupt();Message msg = new Message();msg.what = 1;handler.sendMessage(msg);}} catch (InterruptedException e) {// e.printStackTrace();}Looper.loop();}});threadA.setDaemon(true);threadA.start();//データの獲得ThreadthreadB = new Thread(new Runnable() {@Overridepublic void run() {Looper.prepare();list = countListBll.getAllByJan(handler, productCD);//access DBif (!isTimeOut && !isStopThread&&!Thread.interrupted()) {isAllDone = true;threadA.interrupt();Message msg = new Message();msg.what = 2;handler.sendMessage(msg);}Looper.loop();}});threadB.start();} else {// is not net workif (!currentDialog.isShowing()) {String msg = getString(R.string.MSG_E_0003);String okStr = getString(R.string.MSG_COMMON_OK);showAlertDialog_OK(msg, okStr,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {stopThread();finish();}});}}/** * Threadを閉める */private void stopThread() {if (threadA != null) {threadA.interrupt();}if (threadB != null) {threadB.interrupt();}}/** * UI更新Handler */final Handler handler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case 1:if (isTimeOut && !isAllDone) {Log.i("====", ">>>>>>TimeOut");String msgStr = getString(R.string.MESSAGE_CODE_U0014);String okStr = getString(R.string.MSG_COMMON_OK);showAlertDialog_OK(msgStr, okStr,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {lv_detial.setAdapter(null);}});}break;case 2:if (isAllDone && !isTimeOut) {initListView();}break;case 3:// object has benn closedisStopThread = true;// stop threadA threadBstopThread();String msgStr = getString(R.string.MSG_E_0003);String okStr = getString(R.string.MSG_COMMON_OK);showAlertDialog_OK(msgStr, okStr,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {stopThread();finish();}});break;}super.handleMessage(msg);}};
页: [1]
查看完整版本: android线程超时的例子 多多指教!Skype:chu_jingwei