I wrote an app that implements a web server with multithreading. The main activity has a button to start/stop a thread. When the server is powered on, this thread stays in listening and, if there is a request it creates a new thread to serve it. The app works fine, but now I would use a service instead, so it can work in background.
Actually I've this design (semplified):
WebServer.java
class WebServer implements Runnable{
protected Thread t;
public void start(){
ON=true;
t=new Thread(this,"WebServer");
t.start();
}
public void stop(){
ON=false;
t=null;}
public void run(){
while(ON)
...
}
public isOn(){
return ON;
}
}
DroidServer.java
class DroidServer extends WebServer{
...
}
MyActivity.java
public class MyActivity extends Activity{
ws = new DroidServer(8080,this);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View V){
if(!ws.isOn()){
ws.start();
btn.setText("Stop");
}else{
ws.stop();
btn.setText("Start");
}}});
}
What should I change, to make it use Services? I'd like to extends Service from DroidServer, but this class already extends WebServer... any solutions?