你真的會用AsyncTask嗎?

來源:互聯網
上載者:User

逾時處理

 

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.2cto.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
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.