I want to build a PC remote controller, so i need to at first make a connection to server(that is a java program) and then open a page for controlling the mouse of PC. I want socket connection to be open in several Activities that i switch between them. when i want to use the opened socket in another activity( for sending the information about mouse coordinates to control the PC's mouse), its not working.
I tried to make a new connection in second page too, but didn't work.
In my MainActivity i wrote the code bellow:
public class MainActivity extends AppCompatActivity {
Context context;
EditText IPText;
Button BLock;
private boolean isConnected=false;
private Socket socket;
private PrintWriter out;
String ipaddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this; //save the context to show Toast messages
IPText = (EditText) findViewById(R.id.ipText);
ipaddress = IPText.getText().toString();
BLock = (Button)findViewById(R.id.lockB);
BLock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ConnectPhoneTask connectPhoneTask = new ConnectPhoneTask();
connectPhoneTask.execute(IPText.getText().toString()); //try to connect to server in another thread
}
});
}
@Override
public void onDestroy()
{
super.onDestroy();
if(isConnected && out!=null) {
try {
out.println("exit"); //tell server to exit
socket.close(); //close socket
} catch (IOException e) {
Log.e("remotedroid", "Error in closing socket", e);
}
}
}
public class ConnectPhoneTask extends AsyncTask<String,Void,Boolean> {
@Override
protected Boolean doInBackground(String... params) {
boolean result = true;
try {
InetAddress serverAddr = InetAddress.getByName(params[0]);
socket = new Socket(serverAddr, Integer.parseInt(Constants.SERVER_PORT));//Open socket on server IP and port
} catch (IOException e) {
Log.e("remotedroid", "Error while connecting", e);
result = false;
}
return result;
}
@Override
protected void onPostExecute(Boolean result)
{
isConnected = result;
Toast.makeText(context,isConnected?"Connected to server!":"Error while connecting",Toast.LENGTH_LONG).show();
try {
if(isConnected) {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true); //create output stream to send data to server
Intent i = new Intent(MainActivity.this,mouseActivity.class);
startActivity(i);
}
}catch (IOException e){
Log.e("remotedroid", "Error while creating OutWriter", e);
Toast.makeText(context,"Error while connecting",Toast.LENGTH_LONG).show();
}
}
}
}
And my MouseActivity:
public class mouseActivity extends AppCompatActivity {
private boolean isConnected=false;
TextView mousePad;
private boolean mouseMoved=false;
private PrintWriter out;
private float initX =0;
private float initY =0;
private float disX =0;
private float disY =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mouse);
mousePad = (TextView)findViewById(R.id.mousePad);
//capture finger taps and movement on the textview
mousePad.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(isConnected && out!=null){
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//save X and Y positions when user touches the TextView
initX =event.getX();
initY =event.getY();
mouseMoved=false;
break;
case MotionEvent.ACTION_MOVE:
disX = event.getX()- initX; //Mouse movement in x direction
disY = event.getY()- initY; //Mouse movement in y direction
/*set init to new position so that continuous mouse movement
is captured*/
initX = event.getX();
initY = event.getY();
if(disX !=0|| disY !=0){
out.println(disX +","+ disY); //send mouse movement to server
}
mouseMoved=true;
break;
case MotionEvent.ACTION_UP:
//consider a tap only if usr did not move mouse after ACTION_DOWN
if(!mouseMoved){
out.println(Constants.MOUSE_LEFT_CLICK);
}
}
}
return true;
}
});
}
}
I have problem with "isConnected" and "out" parameters in MouseActivity !!
Do you know how can i fix it??