1

I have a class that extends AsyncTask to send an ethernet message. When created and called within my MainActivity it works fine:

new SendCommandTask(MainActivity.this).execute(ipAddress, portNum, message);

However, I would like to embed this within a method in a new class instead...

public class NewClass{

   String ipAddress;
   String portNum;
   String command;

   public NewClass(String ip, String pn, String, c){
      ipAddress = ip;
      portNum = pn;
      command = c;
   }
   public send(){
      SendCommandTask(MainActivity.this).execute(ipAddress, portNum, command);
   }
}

... and call the method directly from my MainActivity:

NewClass newClass = new NewClass;    
NewClass.send();

I have tried passing the activity as another parameter in the constructor, but the method pukes when I do that. Is there another way to refer to the calling Activity in my NewClass? Or am I stuck running the AsyncTask from the MainActivity? (I hope this makes sense, I'm very new to Java and OO programming in general.) Any help is very much appreciated!

  • I agree with Sparky that you should consider IntentService for this. Otherwise, please post the error you're referring to when you say 'but the method pukes'. – newbyca Sep 05 '12 at 22:28
  • Also, here's a little more info on how activities and async tasks interact: http://stackoverflow.com/questions/12117031/what-happens-to-an-asynctask-when-the-launching-activity-is-stopped-destroyed-wh – newbyca Sep 05 '12 at 22:29

2 Answers2

1

Please see this answer that I wrote a couple of days ago. I think it applies here as well. The main additional consideration is, does your AsyncTask need to be able to outlive its calling Activity or not? If there's any chance that the Activity will be closed or restarted, either kill the task in onStop() or be careful not to use any shared fields.

If your AsyncTask is going to directly read any members that are part of the Activity that you are passing in, you may as well just keep it inside the Activity. Factoring out the task into a freestanding class is best done when you want it to be independent and able to work with several Activity classes.

Also, consider doing your work in a Service instead of an Activity. I'm a big fan of IntentService.

Community
  • 1
  • 1
Sparky
  • 8,437
  • 1
  • 29
  • 41
  • Yes, it definitely sounds like you should be using IntentService. – newbyca Sep 05 '12 at 22:30
  • I only have one Activity in my app (for now, anyway) so it will definitely outlive the AsyncTask, which has a very short lifespan. It just sends a String via ip and then ends. Sounds like I'm better off leaving the call in the activity code. I will definitely explore switching over to a Service, but that's a bit over my head at the moment. One thing at a time :) Thanks for your help! – Holyschnitt Sep 06 '12 at 00:35
0

You could try passing the previous Activity as a context in your new class's constructor.

public NewClass(Context mainActivity, String ip, String pn, String, c)

It might work, but it seems a bit hacky.

corgichu
  • 2,580
  • 3
  • 32
  • 46
  • SendCommandTask requires a parameter of type MainActivity, so I would still need to create a new MainActivity object to use. How would I them re-associate the Context with the MainActivity object? You're right, though. Either way, that seems like an inelegant solution. – Holyschnitt Sep 06 '12 at 01:12