简体   繁体   中英

How to design a Custom Generic Thread class (send and recieve data) in Android

I want to make a Custom Generic Thread Class for heavy network operations.

I know how to use AsyncTask , been using it, but now it sometimes crashes, since AsyncTask is not recommended for operations which take more than 5 seconds.. And for implementing OOP principles (re-usability) I decided to make a separate common Thread class which all my Transaction Screens can use to send and receive data from the network ! I don't want to make a separate thread for each screen.. I searched the internet and the stackoverflow for all possible solutions...

This is what I've tried so far...

import mainMenu.AccountBean;
import parser.ParserMain;
import parser.ValuesBean;

public class NetworkThread implements Runnable {
private String proccessCode;
private AccountBean ab;
private ValuesBean vb;
private String[] sd;

public NetworkThread(String proccessCode, AccountBean ab, String[]sd){
    this.proccessCode = proccessCode;
    this.ab = ab;
    this.sd = sd;

}


@Override
public void run() {
    ParserMain pm = new ParserMain();
    try {
        // this method does socket based transactions..
        vb = pm.commenceTransaction(proccessCode, ab, sd); // I want this object 'vb' sent back to my activity..
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();



     }

  }

}

My SomeActivity's Button Listener

submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            NetworkThread nt = new NetworkThread("1234",ab, someOtherValuesHere);
            Handler h = new Handler();
            h.post(nt); //  <= how to get value from here
        }
    });

My Questions are how to get value from this Handler ?? I've seen many examples on Message but in my scenario where do I implement Message in Activity or the Thread class ? If there is another approach preferred I'm open to suggestions, Thanks.

You aren't running this in a separate Thread , despite the fact that you've named it NetworkThread , it isn't a Thread This is running on the main (UI) thread, which isn't what you want.

Change NetworkThread so that it extends Thread , not Runnable . Then do this:

    @Override
    public void onClick(View v) {
        NetworkThread nt = new NetworkThread("1234",ab, someOtherValuesHere);
        nt.start();
    }

To get data from NetworkThread back to the calling Activity, you could do something like this:

Declare a callback interface in NetworkThread :

public Interface NetworkThreadCallback {
    void reportData(ValuesBean vb);
}

and declare a member variable in NetworkThread :

private NetworkThreadCallback callback;

Then, in NetworkThread constructor, add a parameter NetworkThreadCallback callback and store that in the member variable callback . After you've successfully called commenceTransaction , do this:

callback.reportData(vb);

Each calling Activity needs to implement NetworkThreadCallback and have a method

public void reportData(ValuesBean vb) {
    // Do something with the ValuesBean here...
}

You just need to understand that this technique is not as robust as using a Service . If your process goes to the background (User presses HOME button, takes a phone call, etc.) Android can kill your process at any time. In this case your NetworkThread will just go away.

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