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!!!

no one can help me out?!!!!!!

I would like to have app that would run non-stop and drain energy. I would rather have service that on pre-define time wakes-up do what ever needs to be done and go back to sleep. Have look on this threadd at stackoverflow Service architecture, continuously running vs. wake up

PS: We also have pivate lifes and are here when times allow ;)

Hi Peter,

Thanks for your reply, my apologies, I've been working on this a while now and I keep coming up against problems and to be honest it gets a little frustrating!! To be honest, at the moment I can't really restructure the architecture, I'm very inexperienced when it comes android so I'm happy I've come this far. This is just for demo purposes and will never reach publication. I know it would be better to have something that would sleep when not in use and awake when needed, but for this energy conservation isn't an issue even though it would be the best method.

Would you have any insight how to constantly call this method? I've tried putting it within the while (s.isConnected()) loop but unfortunately this throws up errors. I had thought that once the device is connected, check the distance.

Any thoughts?

Thanks again and apologies if I seemed impatient ;)

What you can do is that once the coordinates are received call on Socket s.close() and that will end while(s.isConnected())

Thanks Peter, that won't work as I need to keep the socket open in order to constantly receive coordinates. I managed to sort it myself though thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.