I have TrackingService component to track the location of the buses in my city based on Crowdsourcing. In the TrackingService class I have variable pLong, pLat
to stored the latitude and longitude when they are calaculated in the onLocatiochChanged()
. The TrackingService is operating in the background, then the data is transmitted to the server. I have an Map Activity to display the location of the buses, the user selected in the MainActivity( as Filter).
The background TrackingService is started in the MainActivity when the app launches.
I am trying to pass the pLat, pLong
from the onLocationChanged()
to the Map
activity when the onLocationChanged()
is being invoked to display the user's current location beside the bus's locations with the aid of BroadcastReceiver.
I have debugged my code and facing problem that I am getting 0.0
for the values lat, lng
in the BroadcastReiceiver in the map
activity also the bReceiver
is being invoked.
How can I get it to work to retrieve the lat and lng vlaues in the BroadcastReceiver bReceiver
?
TrackingService class:
public class TrackingService extends Service implements
LocationListener {
public double pLong;
public double pLat;
...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
detectLocation();
return START_STICKY;
}
private void detectLocation() {
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
this);
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
pLong = location.getLongitude();
pLat = location.getLatitude();
Intent intent = new Intent(Map.RECEIVE_latLng);
Bundle extras = new Bundle();
extras.putDouble("lng", pLong);
extras.putDouble("lat", pLat);
intent.putExtras(extras);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
System.out.println("ABC TrackingService: "+ pLat + " ; " + pLong);
.....
}
}
Map activity:
public class Map extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE_latLng);
bManager.registerReceiver(bReceiver, intentFilter);
}
public static final String RECEIVE_latLng = "com.bustracker.RECEIVE_latLng";
private BroadcastReceiver bReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RECEIVE_latLng)) {
Bundle extras = getIntent().getExtras();
if(!extras.isEmpty() && extras != null){
double lng = extras.getDouble("lng");
double lat = extras.getDouble("lat");
LatLng ll = new LatLng(lng,lat);
MarkerOptions markerOpt = new MarkerOptions().title("My Location")
.position(ll);
System.out.println("ABC map: "+ lat + " ; " + lng);
myLocatMarker = map.addMarker(markerOpt);
}
}
}
};
}
output:
08-27 15:25:48.116: I/System.out(3260): ABC TrackingService: 63.86896885 ; 13.66557022
08-27 15:25:48.146: I/System.out(3260): ABC map: 0.0 ; 0.0
Inside onLocationChanged
Intent intent = new Intent(Map.RECEIVE_latLng);
intent.putExtra("location",location);
sendBroadcast(intent);
onReceive
Location location = intent.getParcelableExtra("location");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.