如果你不想開始一項服務,你可以創建一個全局變量。
創建該插座這樣的Singleton模式:
public class SocketSingleton {
private static Socket socket;
public static void setSocket(Socket socketpass){
SocketSingleton.socket=socketpass;
}
public static Socket getSocket(){
return SocketSingleton.socket;
//return socket;
}
}
然後在代碼只是改變全局值:
public void changeScreen(Socket socket, String ip, int port) {
SocketSingleton.setSocket(socket);
Intent i = new Intent(getActivity().getApplicationContext(), ControlActivity.class);
i.putExtra("ip", ip);
i.putExtra("port",port);
startActivity(i);
}