I get my location in Main Activity regularly by Fused Location Provider each 30 seconds. I don't use service or anything else. i get location just when user is in application.
In Main Activity i have a button that when pressed, goes to the Second Activity and show my current location on the map (By this line: googleMap.setMyLocationEnabled(true)).
1- I want to send my location data from Main Activity to Second Activity regularly(each 30 second that onLocationChange method called), to move my current location's marker. How can i send location data regularly? and where i put codes for this? in onChangedLocation method? (Or it's not necessary at all? and my location move automatically when it's changed?)
Also, i send my current location to server, in OnLocationChanged method, by Volley library like this:
@Override
public void onLocatuionChanged(Location location){
if(location != null)
sendLocation(location);
}
private void sendLocation(Location location){
Map<String, String> params = new HashMap<String, String>();
params.put("latitude", String.valueOf(location.getLatitude();
params.put("longitude", String.valueOf(location.getLongitude();
jsonObjReq = new VolleyCustomReq(Method.POST,
URL, params,
new Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response){
if(response != null)
if(response.optString("type").equalsIgnoreCase("1")
showAlertDialog1();
else if(response.optString("type").equalsIgnoreCase("2")
showAlertDialog2();
else if(response.optString("type").equalsIgnoreCase("3")
showAlertDialog3();
else if(response.optString("type").equalsIgnoreCase("addNewMarker")
addNewMarkerOnMapAsDestinationAndMakePathBetweenBothOfYou();
...
}
...
}
}
If there is a response from server, depending on the response type, i want to do something; for example show related message to user, add a marker as destination on the map and make path between two markers.
2- Does onLocationChanged method works even when we are in another activities?
3- If yes, Does AlertDialog can show in every activities? (not only in Main Activity that showAlertDialog method and LocationListener are there).
4- Is there better way to handle responses and do related stuffs?
There's only one activity active and running at any time. You might be better off with 2 fragments or just an additional MapView in your MainActivity which you can make visible or gone, instead of having different activities.
Or you use a service, which can run in the background in parallel to any of your Activities.