Android 中线程网络超时的处理

Android开发中经常需要调用线程访问网络,而手机的网络信号经常断断续续,容易出现网络超时的情况,这种情况下后台线程往往得不到关闭,浪费系统资源。

在下面的例子中使用了java 中的Timer类,对线程进行了约束,如果线程在一定时间内为响应则终止该线程。

[java][/java] view plaincopy
  1. package com.zf.thread_test;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.app.Activity;
  8. import android.app.ProgressDialog;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.Toast;
  14. public class MainActivity extends Activity {
  15.     private static final int TIME_OUT = 0;
  16.     private static final int SUCCESS = 1;
  17.     // 超时的时限为5秒
  18.     private static final int TIME_LIMIT = 5000;
  19.     ProgressDialog proDialog;
  20.     Timer timer;
  21.     Thread thread;
  22.     Button btn1, btn2;
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.         btn1 = (Button) findViewById(R.id.button1);
  28.         btn2 = (Button) findViewById(R.id.button2);
  29.         // 测试未超时的线程,为btn1绑定事件
  30.         btn1.setOnClickListener(new OnClickListener() {
  31.             @Override
  32.             public void onClick(View v) {
  33.                 proDialog = ProgressDialog.show(MainActivity.this, “提示”,
  34.                         “线程运行中”);
  35.                 // 匿名内部线程
  36.                 thread = new Thread() {
  37.                     @Override
  38.                     public void run() {
  39.                         while (true) {
  40.                             try {
  41.                                 //线程休眠时间,超时
  42.                                 sleep(10000);
  43.                             } catch (InterruptedException   e) {
  44.                                 break;
  45.                             }
  46.                         }
  47.                     }
  48.                 };
  49.                 thread.start();
  50.                 // 设定定时器
  51.                 timer = new Timer();
  52.                 timer.schedule(new TimerTask() {
  53.                     @Override
  54.                     public void run() {
  55.                         sendTimeOutMsg();
  56.                     }
  57.                 }, TIME_LIMIT);
  58.             }
  59.         });
  60.         // 测试超时的线程,为btn2绑定事件
  61.         btn2.setOnClickListener(new OnClickListener() {
  62.             @Override
  63.             public void onClick(View v) {
  64.                 proDialog = ProgressDialog.show(MainActivity.this, “提示”,
  65.                         “线程运行中”);
  66.                 // 匿名内部线程
  67.                 thread = new Thread() {
  68.                     public void run() {
  69.                         try {
  70.                             // 线程休眠时间,未超时
  71.                             Thread.sleep(3000);
  72.                         } catch (InterruptedException e) {
  73.                             e.printStackTrace();
  74.                         }
  75.                         Message msgSuc = new Message();
  76.                         msgSuc.what = SUCCESS;
  77.                         myHandler.sendMessage(msgSuc);
  78.                     }
  79.                 };
  80.                 thread.start();
  81.                 // 设定定时器
  82.                 timer = new Timer();
  83.                 timer.schedule(new TimerTask() {
  84.                     @Override
  85.                     public void run() {
  86.                         sendTimeOutMsg();
  87.                     }
  88.                 }, TIME_LIMIT);
  89.             }
  90.         });
  91.     }
  92.     // 接收消息的Handler
  93.     final Handler myHandler = new Handler() {
  94.         public void handleMessage(android.os.Message msg) {
  95.             switch (msg.what) {
  96.             case TIME_OUT:
  97.                 //打断线程
  98.                 thread.interrupt();
  99.                 proDialog.dismiss();
  100.                 Toast.makeText(MainActivity.this, “线程超时”, Toast.LENGTH_SHORT)
  101.                         .show();
  102.                 break;
  103.             case SUCCESS:
  104.                 //取消定时器
  105.                 timer.cancel();
  106.                 proDialog.dismiss();
  107.                 Toast.makeText(MainActivity.this, “线程运行完成”, Toast.LENGTH_SHORT)
  108.                         .show();
  109.                 break;
  110.             default:
  111.                 break;
  112.             }
  113.         };
  114.     };
  115.     //向handler发送超时信息
  116.     private void sendTimeOutMsg() {
  117.         Message timeOutMsg = new Message();
  118.         timeOutMsg.what = TIME_OUT;
  119.         myHandler.sendMessage(timeOutMsg);
  120.     }
  121. }

标签