Click here to Skip to main content
15,886,565 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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!!!
Posted
Comments
Prasad Khandekar 12-Apr-13 15:43pm    
Hello Gary,

In your Client B app, you can use LocationManager.getLastKnownLocation(String) api instead of MyLocationListener. This api returns a Location indicating the data from the last known location fix obtained from the given provider. No need to even call onLocationChanged in a loop. Call this api from within your runnable.

Regards,
GaryDoo 13-Apr-13 9:35am    
Hi Prasad,

Thank you for your help, I've attempted what I have under stood from your reply. Something like this? String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
Platitude = location.getlatitude(); Plongitude = location.getLongitude(); 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); All within my runnable rec? (apologies for the layout of my reply, it won't allow me to format my input)


Or is there another option?
GaryDoo 13-Apr-13 13:03pm    
@Prasad, could I just put distance = child.distanceTo(parent) within the while (s.isConnected()) loop? That way, when the socket is connected is will repeatedly get checked? Or could you see any potential issues with this?

1 solution

Hi,

try to use timer control tick event to fire that function in continous time interval,

set following properties

C#
imer.Interval = (1000) * (10);             // Timer will tick evert 10 seconds
            timer.Enabled = true;                       // Enable the timer
            timer.Start();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900