java - Web Server: use Service instead of Thread -


I have written an app that implements a web server with multithreading. The main activity has a button to start / stop a thread. When the server is on, it stays in listening to the thread and, if there is a request, it creates a new thread to serve it. The app works fine, but now I use a service instead, so it can work in the background.

Actually, I have done this design:

Web server Java

  Sets the class webserver Runnabal {Secure thread t; Public Zero Starter () {ON = True; T = new thread (this, "webserver"); T.start (); } Public Zero Stop () {ON = false; T = null;} Public Zero Run () {while (current) ...} is public () {upon return; }}   

DroidServer.java

  class extends the DroidServer webserver {...}  < / Pre> 

MyActivity.java

  increases the MyActivity activity of the public class {ws = new DroidServer (8080, this); Btn.setOnClickListener (New OnClickListener) {Click on Public Zero (see V) {if (! Ws.isOn ()) {ws.start (); BT.N.Set Text ("Stop");} and {Yststop ( ); Btn.setText ("Start");}}}); }   

What should I change to use the services? I want to extend the service to DroidServer, but this class has already expanded any solution to the webserver ...?

Service is not a replacement for a thread from per definition:

The most confusions about the service class revolve in reality which is not:

  • No service is a separate process, the service object does not mean that it is in its process ; Unless otherwise specified, it runs in the same process as it is a part of it.
  • No service is a thread. It does not make sense to work on the main thread (no answer to errors to avoid the application)

    What should you do to make this a service, then run your DroidServer in service to run as a separate thread in order to accept the request in the background

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -