Android的重新启动的AsyncTask [英] Android restart AsyncTask

查看:192
本文介绍了Android的重新启动的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦重启的的AsyncTask 之后,我尝试重新打开该活动。

I am having some trouble restarting an AsyncTask after I try to reopen the activity.

当我第一次打开我把这个活动启动的AsyncTask 这工作的第一次。

When I first open the activity I call this to start the AsyncTask which works the very first time.

myTask connectedTask;
connectedTask = new myTask();
connectedTask.execute();

public class myTask extends AsyncTask<Integer,Integer, Integer> {

    @Override
    protected Integer doInBackground(Integer... arg0) {
        //Increase timer and wait here in a loop
        System.out.println("ASYNC TASK STARTING!");
        return IsSocketConnected();
    }

    protected void onPostExecute(Integer result) {
        //Something you want to do when done?
        System.out.println("ASYNC TASK DONE!");

        // if it was connected successfully 
        if(result == 1) {
            // remove the progress bar
            proBar.setVisibility(View.GONE);

            // Discover available devices settings and create buttons
            CreateButtons(btnList);
        }
    }
}

IsSocketConnected(); // checks for a bluetooth connections to be done. 

当我回到previous活动,并试图启动活动我再不能得到的的AsyncTask 重新开始。

When I go back to previous activity and try to start that activity again I can't get the AsyncTask to start again.

我看了,只要我创造的一个新实例的AsyncTask 我要重新启动这些任务。

I read that as long as I create a new instance of the AsyncTask I should be to restart those tasks.

有没有别的东西,我应该做的事?

Is there something else that I should be doing?

感谢您,

推荐答案

感谢您对您的所有意见,他们帮了不少忙。我决定废除与AsyncTask的。最后我用一个正常运行的线程和使用处理程序来发布消息回到UI线程。这里是code:

Thank you for all your comments they helped a lot. I decided to do away with the AsyncTask. I ended using a normal runnable Thread and using Handlers to post messages back to the UI thread. here is the code:

        // Start thread here only for IsSocketConnected
        new Thread(new Runnable() {

            public void run() {

                //Add your code here..
                IsSocketConnected();

            }
        }).start();


// handler that deals with updating UI
public Handler myUIHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        if (msg.what == Bluetooth.STATE_CONNECTED)
        {
            //Update UI here...
            Log.d(TAG, "Connected");




            // Discover available devices settings and create buttons
            CreateButtons(btnList);

        } else if(msg.what == Bluetooth.STATE_NONE) {

            Log.d(TAG, "NOT Connected");
        }

}

// in the IsSocketConnected() I call this 
Message theMessage = myUIHandler.obtainMessage(Bluetooth.STATE_CONNECTED);
myUIHandler.sendMessage(theMessage);//Sends the message to the UI handler.

这是工作至今。再次感谢你。希望这可以帮助别人

This is working so far. Thank you again. Hope this helps someone

这篇关于Android的重新启动的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆