A little confused about the need for a context when setting up this class shown below. Do I need context when making an instance of this class to use it in my Activity?
I can set up the constructor for the class one way like this:
public Server(Context myContext)
or without context as a parameter, not using context:
public Server()
now for more of the code from the Server class:
public class Server {
private TextView serverStatus;
public TextView receivedCommand;
public String line = null;
// default ip
// public static String SERVERIP = "192.168.1.001";
public static String SERVERIP = "localhost";
// designate a port
public static final int SERVERPORT = 8080;
private Handler handler = new Handler();
private ServerSocket serverSocket;
public Server(){
SERVERIP = getLocalIpAddress();
Thread fst = new Thread(new ServerThread());
fst.start();
}
public class ServerThread implements Runnable {
// ... the rest of the code
If your class extends
android.app.Servicethen you already have access togetApplicationContext()but having said that theContextisn’t essential however we commonly use it to access application preferences and directories from inside our Services.