WEB开发网
开发学院手机开发Android 开发 Android 线程超时的例子 阅读

Android 线程超时的例子

 2010-11-05 00:56:40 来源:WEB开发网   
核心提示:private Handler mHandler = new Handler() {public void handleMessage(Message msg) {switch(msg.what){case TIMER_EXECUTE:if (eThread.getState().toString().equals(&

private Handler mHandler = new Handler() {

public void handleMessage(Message msg) {

switch(msg.what){

case TIMER_EXECUTE:

if (eThread.getState().toString().equals("TERMINATED")||

eThread.getState().toString().equals("TIMED_WAITING")) {

eThread.stopThread(true);

showDialog(ERROR_MESSAGE);

timer.cancel();// 关闭计时器

}

break;

}

super.handleMessage(msg);

}

};

protected void checkThread() {

Message msg = new Message();

msg.what = TIMER_EXECUTE;

mHandler.sendMessage(msg);

}

class EThread extends Thread {

private boolean flag = true;

public void stopThread(boolean flag) {

this.flag = !flag;

}

@Override

public void run() {

Looper.prepare();

try {

Thread.sleep(6000);

} catch (InterruptedException e) {

e.printStackTrace();

}

timer.cancel();// 关闭计时器

if(!flag) {

return;

}

Intent i = new Intent(Main.this,SecondActivity.class);

startActivity(i);

}

}

}

import java.util.Timer;

import java.util.TimerTask;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.os.Looper;

import android.os.Message;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

/**

* @author Tony Shen

*

*/

public class Main extends Activity {

上一页  1 2 3 4  下一页

Tags:Android 线程 超时

编辑录入:coldstar [复制链接] [打 印]
赞助商链接