简体   繁体   English

如何从外部调用方法到AsyncTask类中?

[英]How to call a method into an AsyncTask class from outside?

I have two simple classes: 我有两个简单的类:

public class MainActivity extends Activity {
    NetworkTask task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        [...]       
        task = new NetworkTask();
        task.execute();
    }

public void myClickHandler(View view) {
    switch(view.getId()) {
        case R.id.button1:
            // Why this line crash?
            task.connection("127.0.0.1");
        break;
        }
    }
}

and

public class NetworkTask extends AsyncTask<String, Void, String> {
    Socket sock;
    volatile boolean running = true;

    public int connection(String url){
        try{
            sock = new Socket(url, 4567)
        }
        catch (IOException ex){
            Logger.getLogger(NetworkTask.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        }
    }

    public String doInBackground(String... strings) {

        // If I do this, it works well
        //connection(127.0.0.1);

        while(running)
        {
            [...]
        }

        return null;
    }   
}

As I commented when I call connection method from outside of the AsyncTask method, the app crashes more particulary « sock = new Socket(...) » line. 正如我在从AsyncTask方法外部调用连接方法时所评论的那样,该应用程序更具体地崩溃了«sock = new Socket(...)»行。 But when connection call is done inside the AsynTask method socket is created. 但是,当在AsynTask方法套接字内部创建连接调用时,就会创建该套接字。 I don't understand why. 我不明白为什么。

What's happening? 发生了什么?

Thanks. 谢谢。

It's because when you do 因为当你这样做

task.connection("127.0.0.1");

You are still working in the main (UI) Thread - you're not using the AsyncTask properly. 您仍在主(UI)线程中工作-您未正确使用AsyncTask。 Instead you're using it like a normal class, and so, you get a NetworkOnMainThreadException on the new Android versions. 相反,您像普通类一样使用它,因此,在新的Android版本上会收到NetworkOnMainThreadException

However when you call from doInBackground() , it means you started the AsyncTask via execute and the work is done in a separate Thread, letting everything work as it should. 但是,当您从doInBackground()调用时,这意味着您是通过execute启动AsyncTask的,并且工作是在单独的 Thread中完成的,从而使一切正常工作。

Keep in mind that if you are doing non-network stuff, you can still call from outside. 请记住,如果您正在做非网络事务,您仍然可以从外面打电话。 However, I'd recommend keeping your AsyncTask depend on the outside as little as possible, since AsyncTasks only run once. 但是,由于AsyncTasks仅运行一次,因此建议您尽量减少AsyncTask的外部依赖。 You then have to make a new instance if you want to do more work, which means if you depend on setter methods or similar, you have to make sure you call those methods again, which makes this simple class more complex than needed. 然后,如果您想做更多的工作,则必须创建一个新实例,这意味着,如果您依赖于setter方法或类似方法,则必须确保再次调用这些方法,这使此简单的类变得比所需的更为复杂。

For a good, to the point explanation of how to use an AsyncTask, this is a pretty good source. 很好的讲解了如何使用AsyncTask, 是一个很好的资源。 And of course the official documentation . 当然还有官方文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM