android登录超时显示demo

package com.xunfang.epay_test;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		TimeoutAsy asy = new TimeoutAsy();
		asy.execute("");
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				Toast.makeText(MainActivity.this, "超时", Toast.LENGTH_SHORT)
						.show();
			}
		};
	};

	private void timeoutTest() {
		// TODO Auto-generated method stub
		new Timer().schedule(new TimerTask() {

			@Override
			public void run() {
				Message msgMessage = new Message();
				msgMessage.what = 1;
				handler.sendMessage(msgMessage);
			}
		}, 2 * 1000);
	}
/**
 * 参数:启动输入参数,后台执行百分比,后台执行返回解果
 * @author smalt
 *
 */
	class TimeoutAsy extends AsyncTask {

		
		@Override
		protected String doInBackground(String... params) {
			timeoutTest();
			return null;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// 显示进度框
			dialog = ProgressDialog
					.show(MainActivity.this, null, "请等待..", true);
		}
	}
}

你可能感兴趣的:(android)