简体   繁体   中英

Android Async Task and TCP/IP Socket

I have a basic question about Async task. I'm a beginner in Android programming, sorry for this question.

I'm going to open a socket in doinbackground.

  doInBackground(... ) {
    Socket socket = null;
    DataOutputStream dataOutputStream = null;
    DataInputStream dataInputStream = null;
    try {
        socket = new Socket(192.168.0.1, 2000);
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataInputStream = new DataInputStream(socket.getInputStream());
  }}

What happens to a socket when AsyncTask has finished? (As soon as doInBackground and OnPostExecute has passed.)

Is the Socket still available? Or will it removed by the Garbage Collector?

Next question, but actually the same background.

What happens to an instance of a class which I Instantiate in doInBackground after AsyncTask has finished? (As soon as doInBackground and OnPostExecute has passed.)

doInBackground(... ) {
IPConnection ipcon = new IPConnection();
}

---------------------------------------------------------------------

Edit:

How can I create a reference from a object in Asynctask to the MainActivity?

Edit2:

Is that a reference to the main thread? Would the objects not be removed by the Garbage Collector in that Code Example?

public class ClientActivity extends Activity {


private IPConnection ipcon;

private Socket Testsocket;

public class IPConnection extends AsyncTask<String, String, IPConnection> {

    @Override
    protected IPConnection doInBackground(String... message) {

         ipcon = new IPConnection();


        ipcon.run();

        return null;
    }

  }

}

Thank you in advance.

Is the Socket still available? Or will it removed by the Garbage Collector?

No the socket will be unavailable and will be removed by the garbage collector because it doesn't hold any reference

What happens to an instance of a class which I Instantiate in doInBackground after AsyncTask has finished? (As soon as doInBackground and OnPostExecute has passed.)

same as above the ipconnection doesn't hold any reference so it'll be collected by garbage collector

if you want to pass it to the activity you can create an interface

public interface AsyncResultPasser {
    void passSocket(Socket socket);
    void passIPconnection(IPConnection ipcon);
}

and then in your asynctask class you have to add

public AsyncResultPasser delegate = null;

and don't forget to set it first before you execute your asynctask

public class YourActivity implements AsyncResponse{
   YourAsyncTask asyncTask = new YourAsyncTask ();
    @Override
    public void onCreate(Bundle savedInstanceState) {
     asyncTask.delegate = this;
    }


   void passSocket(Socket socket){
     //you can get your socket here
   }

   void passIPconnection(IPConnection ipcon){
     //you can get your ipconnection here
   }
}

and to call it just simply use delegate.passSocket(socket) and delegate.passIPconnection(ipcon)

I hope my answer can helps you :)

As soon as doInBackground() finishes, all local instances will be available for garbage collection, unless you pass one of those to onPostExecute() by returning it form doInBackground() . Such instances will be available after onPostExecute() finishes. But again only if you don't send these instances further somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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