Hi folks,
I've a quick question, I have developed an application that transmits coordinates from one device (Client A) to another (Client B) and gets the distance between both, I am using the distanceTo() to get the distance, this is calculated on Client B. However this is only called in the onLocationChanged in Client B, and the idea is that Client A will be roaming and Client B will be almost stationary. So checking the distance in Client B onLocationChanged isn't ideal.
Is there a way to constantly call the distanceTo() method, if I put it in a while(true) loop, it will never exit and the next line of code won't be executed. I'd appreciate a dig out on this if possible here's some of my client B code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.parent);
submit = (Button) findViewById(R.id.buttonSubmit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
happiness.add((Integer) item);
}
});
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.happiness, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = (Spinner) findViewById(R.id.happy);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
LocationManager mlocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocationListener = new MyLocationListener();
mlocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, mlocationListener);
Thread rec = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {
s = new Socket("192.168.1.2", 1980); // Connecting to Server
//s = new Socket("86.46.233.230", 1980);
InputStream fromServer = s.getInputStream();
if (!s.isConnected()) {
Toast.makeText(getApplicationContext(),
"No Connection", Toast.LENGTH_LONG).show();
}
while (s.isConnected()) {
Scanner r = new Scanner(fromServer);
if (r.hasNextLine()) {
location = r.nextLine();
}
String[] currLocation = location.split(","); // STRING IS SPLIT EACH SIDE OF COMMA
Clatitude = Double.valueOf(currLocation[0]); // TAKING FIRST ELEMENT OF ARRAY
Clongitude = Double.valueOf(currLocation[1]); // TAKING SECOND ELEMENT OF ARRAY
// IN ORDER TO RUN ON THE UI THREAD
mHandler.post(new Runnable() {
public void run() {
TextView myTextview1 = (TextView) findViewById(R.id.childCo);
myTextview1.setText(location);
}
});
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
rec.start();
}
// =====================================================================================
public class MyLocationListener implements LocationListener {
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled",
Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Platitude = location.getLatitude();
Plongitude = location.getLongitude();
//Adding parent coordinates to string for printing - (testing)
ParentCoordinates = (Platitude + " , " + Plongitude);
TextView myTextview2 = (TextView) findViewById(R.id.parentCo);
myTextview2.setText(ParentCoordinates);
Location child = new Location("point A"); //INITIALIZING CHILD LOCATION
child.setLatitude(Clatitude); //SETTING CHILD LOCATION LAT & LONG
child.setLongitude(Clongitude);
Location parent = new Location("point B"); //INITIALIZING CHILD LOCATION
parent.setLatitude(Platitude); //SETTING CHILD LOCATION LAT & LONG
parent.setLongitude(Plongitude);
float distance = child.distanceTo(parent); //GETTING DISTANCE FROM PARENT LOCATION TO CHILD LOCATION
String dis = ("" + distance);
TextView myTextview1 = (TextView) findViewById(R.id.distTo); //DISPLAYING DISTANCE IN TEXT VIEW
myTextview1.setText(dis);
}
// =====================================================================================
}
// =====================================================================================
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// TODO Auto-generated method stub
item = parent.getItemAtPosition(pos); //GETTING ITEM SELECTED FROM SPINNER
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
// ======================================================================================
}
Thanks in advance folks!!!