Aditya Chawla Aditya Chawla - 4 months ago 32
Android Question

Google locations api location listener

I am using google location api for my app and there is a location listener in it-

@Override
public void onLocationChanged(Location location) {
if (paused){
Runnable r = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
if (alarm){
double distance;
Location locationA = new Location("");
locationA.setLatitude(destlat);
locationA.setLongitude(destlng);
Location locationB = new Location("");
locationB.setLatitude(updLat);
locationB.setLongitude(updLng);
distance = locationA.distanceTo(locationB);
current_distance.setText("Current distance: " + Math.round(distance) + " m");
if(distance<rad){
alertUser();
alarm = false;
}
}
}
});
}
};
Thread bgAlarmThread = new Thread(r);
bgAlarmThread.start();
}else{
updLat = location.getLatitude();
updLng = location.getLongitude();
locll = new LatLng(updLat,updLng);

MarkerOptions locoptions = new MarkerOptions()
.title("You are here")
.position(locll)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mapmarker));

if (locmarker == null){
gotoLocation(updLat, updLng, 15);
}

if(locmarker != null){
locmarker2 = locmarker;
locmarker2 = map.addMarker(locoptions);
locmarker.remove();
locmarker = null;
}
locmarker = map.addMarker(locoptions);
if(locmarker2 != null){
locmarker2.remove();
locmarker2 = null;
}



if (alarm){
double distance;
Location locationA = new Location("");
locationA.setLatitude(destlat);
locationA.setLongitude(destlng);
Location locationB = new Location("");
locationB.setLatitude(updLat);
locationB.setLongitude(updLng);
distance = locationA.distanceTo(locationB);
current_distance.setText("Current distance: " + Math.round(distance) + " m");
if(distance<rad){
alertUser();
Toast.makeText(this,"Destination is now within your range",Toast.LENGTH_LONG).show();
alarm = false;
}
}

if (frag2.getVisibility() == View.VISIBLE){
double distance;
Location locationA = new Location("");
locationA.setLatitude(destlat);
locationA.setLongitude(destlng);
Location locationB = new Location("");
locationB.setLatitude(updLat);
locationB.setLongitude(updLng);
distance = locationA.distanceTo(locationB);


EditText current_distance = (EditText)findViewById(R.id.currentDistance);
current_distance.setText("Current distance: " + Math.round(distance) + " m");
}




}

}


For my app to work, the location listener should work even when app is minimised, but when my I hit the home button, the location listener would not work. How can I make the listener work when the app is minimized? Is it possible to run the listener itself on an alternative thread? How so?

Answer

Well no-one seems to know the answer so I might as well give it since it's been a long time and it's still unanswered.

public void startTimer() {
    //set a new Timer
    timer = new Timer();
    //initialize the TimerTask's job
    initializeTimerTask();
    //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
    timer.schedule(timerTask, 2000, 4000); //
}
public void initializeTimerTask() {
    timerTask = new TimerTask() {
        public void run() {

            //use a handler to run a toast that shows the current timestamp
            handler.post(new Runnable() {
                public void run() {
                    Runnable r = new Runnable() {
                        @Override
                        public void run() {
                            handler.post(new Runnable() {
                                public void run() {
                                    if (alarm){
                                        double distance;
                                        Location locationA = new Location("");
                                        locationA.setLatitude(destlat);
                                        locationA.setLongitude(destlng);
                                        Location locationB = new Location("");
                                        locationB.setLatitude(updLat);
                                        locationB.setLongitude(updLng);
                                        distance = locationA.distanceTo(locationB);
                                        current_distance.setText("Current distance: " + Math.round(distance) + " m");
                                        if(distance<rad){
                                            alertUser();
                                            alarm = false;
                                        }
                                    }
                                }
                            });
                        }
                    };
                    Thread bgAlarmThread = new Thread(r);
                    bgAlarmThread.start();
                }
            });
        }
    };

}
Comments