Issue
I'm about to start making an app that's supposed to act as a tcp chat client and I've been reading and reading and I've come to the conclusion that it would be best (if not required?) to put my socket and reader in asynctask.
Thing is I'm not sure on where to begin, as I'm new to android this is a massive undertaking for me at least but the basics as far as I've understood it.
Instantiate the views in the mainclass and create a method for sending text from a EditText to the async class.
Create the async class, with the socket and the connection, place the reader here and the writer, the writer will be a method to take in a string parameter which is sent through the socket.
Somehow make my reader constantly listen to the server socket and update my textview with strings that are sent out.
Is this a good basic "plan" to start programming for or is it flawed somehow or me missing something? One question that I got before even starting is the async task, do I make it ? As far as I'm aware I don't need to return anything and I won't be needing any progress updating? My address for the client will be hardcoded so I'm guessing that's the only thing I would want to pass a parameter to the async class?
Sorry if its basic but I'm just starting and would like to not start off wrong.
Solution
You only want to use an AsyncTask for small (relatively) and direct actions, such as downloading specific items (eg. Images or mp3's). It sounds like what you want is, instead, a Service. The service will run in the background of your application and you can handle your network actions through the service.
For other helpful documentation and help on seeing if the Service is right for you, look here.
Edit:
For more information of creating a Service in its own Thread, refer to this.
Edit 2:
Try to think of is as the anti-Activity. It does everything that an activity should not have to do, primarily, processing. Any hardcore processing should be done in a threaded service for many reasons, primarily for multi-tasking and taking processing off the main UI thread. To demostrate this, think of Pandora. Pandora creates a service (in its own thread) that connects to its site and streams music. The service has a life cycle that will allow it to live while the activities are killed. This allows the music to stream will the user does other tasks. But when the user wants to look at what is playing, she will only have to click into the app. From there the activity will check the service, see that it has some data, and base its content off of the data in the service.
I hope this helps. I really can't think of a better why to put it....
Answered By - ahodder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.