android asynctask用法 - 在AsyncTask中使用等待




asynctask教學 cancel (6)

你有這樣的方式與asyntask和wait();

public class yourAsynctask extends AsyncTask<Void, Void, Void> {
    public boolean inWait;
    public boolean stopWork; 

    @Override
    protected void onPreExecute() {
        inWait = false;
        stopWork = false;
    }

    @Override
    protected Void doInBackground(Void... params) {
        synchronized (this) {
            while(true) {
                if(stopWork) return null;
                if(youHaveWork) {
                    //make some
                } else {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }

    public void mynotify() {
        synchronized (this) {
            if(inWait) {
                notify();
                inWait = false;
            }
        }
    }

    public void setStopWork() {
        synchronized (this) {
            stopWork = false;
            if(inWait) {
                notify();
                inWait = false;
            }
        }
    }
}

AsyncTask使用wait時,我得到ERROR/AndroidRuntime(24230): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()

是否可以使用Asynctask來等待? 怎麼樣?

謝謝

class WaitSplash extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        try {
            wait(MIN_SPLASH_DURATION);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }       

    protected void onPostExecute() {
        waitSplashFinished = true;
        finished();
    }
}  

您可以使用Thread.sleep方法

    try {
        Thread.sleep(1000);         
    } catch (InterruptedException e) {
       e.printStackTrace();
    }

為此使用線程

public class SplashActivity extends Activity{

int splashTime = 5000;
private Thread splashThread;
private Context mContext;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.mContext = this;
    setContentView(R.layout.splash_layout);
    splashThread = new Thread(){
        public void run() {
            try{
                synchronized (this) {
                    wait(splashTime);
                }
            }catch(InterruptedException ex){
                ex.printStackTrace();
            }finally{
                Intent i = new Intent(mContext,LocationDemo.class);
                startActivity(i);
                stop();
            }
        }
    };

    splashThread.start();
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        synchronized (splashThread) {
            splashThread.notifyAll();
        }
    }
    return true;
}

在觸摸事件,線程得到通知..可以根據您的需要改變。


@Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                Thread.currentThread();
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;

        }

如果你只是推遲一個方法的執行時間,一個好的選擇是Handler.postDelayed()

定義處理程序和可運行...

private Handler handler = new Handler();
private Runnable runnable = new Runnable() {        
    finished();
};

並延遲執行...

handler.postDelayed(runnable, MIN_SPLASH_DURATION);

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

在我的情況下失踪了。







android android-asynctask wait