简体   繁体   English

Android异步任务和TCP / IP套接字

[英]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. 我是Android编程的初学者,对不起这个问题。

I'm going to open a socket in doinbackground. 我打算在doinbackground打开一个socket。

  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? AsyncTask完成后套接字会发生什么? (As soon as doInBackground and OnPostExecute has passed.) (只要doInBackground和OnPostExecute已通过。)

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? 在AsyncTask完成后,我在doInBackground中实例化的类的实例会发生什么? (As soon as doInBackground and OnPostExecute has passed.) (只要doInBackground和OnPostExecute已通过。)

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

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

Edit: 编辑:

How can I create a reference from a object in Asynctask to the MainActivity? 如何从Asynctask中的对象创建引用到MainActivity?

Edit2: 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? 在AsyncTask完成后,我在doInBackground中实例化的类的实例会发生什么? (As soon as doInBackground and OnPostExecute has passed.) (只要doInBackground和OnPostExecute已通过。)

same as above the ipconnection doesn't hold any reference so it'll be collected by garbage collector 与上面相同,ipconnection没有任何引用,所以它将由垃圾收集器收集

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 然后在你的asynctask类中你必须添加

public AsyncResultPasser delegate = null;

and don't forget to set it first before you execute your asynctask 并且在执行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) 并调用它只需使用delegate.passSocket(socket)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() . 一旦doInBackground()完成后,所有本地实例将可用于垃圾收集,除非你这些信息传递给一个onPostExecute()通过返回它形成doInBackground() Such instances will be available after onPostExecute() finishes. onPostExecute()完成后,这些实例将可用。 But again only if you don't send these instances further somewhere. 但是,只有当你不在某个地方进一步发送这些实例时。

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

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