Are you sure you want to use AsyncTask?

Source: Internet
Author: User

Timeout Processing

 

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(cmd.exe cute ();
018 new cancelselfwhentimeouttask(cmd.exe cute ();
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, "task completed", 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, "task timeout ",
079 Toast. LENGTH_SHORT). show ();
080} else {
081 Toast. makeText (TimeoutTestActivity. this, "task completed ",
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}

Author: Dean83
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.