你真的會用AsyncTask嗎?

超時處理

 

001 import java.util.Timer;
002 import java.util.TimerTask;
003 
004 import android.app.Activity;
005 import android.os.AsyncTask;
006 import android.os.Bundle;
007 import android.widget.Toast;
008 
009 public class TimeoutTestActivity extends Activity {
010     private final static int TIME_OUT = 3 * 1000;
011     private final static int SLEEP_TIME = 2 * 1000;
012 
013     @Override
014     public void onCreate(Bundle savedInstanceState) {
015         super.onCreate(savedInstanceState);
016         setContentView(R.layout.main);
017         //new TimeOutTask().execute();
018         new CancelSelfWhenTimeOutTask().execute();
019     }
020 www.aiwalls.com
021     private class CancelSelfWhenTimeOutTask extends AsyncTask<Void, Void, Void> {
022         private boolean done = false;
023 
024         @Override
025         protected Void doInBackground(Void… params) {
026             cancelSelfWhenTimeOut();
027             sleep();
028             return null;
029         }
030 
031         @Override
032         protected void onPostExecute(Void result) {
033             super.onPostExecute(result);
034             Toast.makeText(TimeoutTestActivity.this, "任務完成", Toast.LENGTH_SHORT)
035                     .show();
036         }
037 
038         private void cancelSelfWhenTimeOut() {
039             new Timer().schedule(new TimerTask() {
040 
041                 @Override
042                 public void run() {
043                     if (!done) {
044                         CancelSelfWhenTimeOutTask.this.cancel(true);
045                     }
046                 }
047             }, TIME_OUT);
048         }
049 
050         private void sleep() {
051             try {
052                 Thread.sleep(SLEEP_TIME);
053                 done = true;
054             } catch (InterruptedException e) {
055             }
056         }
057     }
058 
059     private class TimeOutTask extends AsyncTask<Void, Void, Void> {
060         private boolean done = false;
061         private boolean isTimeOut = false;
062 
063         @Override
064         protected Void doInBackground(Void… params) {
065             try {
066                 throwTimeOutException();
067             } catch (Exception e) {
068                 isTimeOut = true;
069             }
070             sleep();
071             return null;
072         }
073 
074         @Override
075         protected void onPostExecute(Void result) {
076             super.onPostExecute(result);
077             if (isTimeOut) {
078                 Toast.makeText(TimeoutTestActivity.this, "任務超時",
079                         Toast.LENGTH_SHORT).show();
080             } else {
081                 Toast.makeText(TimeoutTestActivity.this, "任務完成",
082                         Toast.LENGTH_SHORT).show();
083             }
084         }
085 
086         private void throwTimeOutException() {
087             new Timer().schedule(new TimerTask() {
088 
089                 @Override
090                 public void run() {
091                     if (!done) {
092                         isTimeOut = true;
093                     }
094                 }
095             }, TIME_OUT);
096         }
097 
098         private void sleep() {
099             try {
100                 Thread.sleep(SLEEP_TIME);
101                 done = true;
102             } catch (InterruptedException e) {
103             }
104         }
105     }
106 
107 }

 作者: Dean83
 

發佈留言