Reputation: 422
I need to gather longitude/latitude location from a service I created on the app to all the classes requiring it.
I want to do this via Intent getDouble call, but although the service works properly and updates the values every 30 seconds, the getDouble call does not receive any value from the intent.
The service part:
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
lat = String.valueOf(location.getLatitude());
lon = String.valueOf(location.getLongitude());
// This shows a string on the phone every time location is updated. Use only for debug
String msg = "GPSService has Updated the Location: " +
Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude());
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
Log.d("Loc updated GPSService","after 30");
// Send Broadcast
intent.putExtra("latitude",mLastLocation.getLatitude());
intent.putExtra("longitude",mLastLocation.getLongitude());
sendBroadcast(intent);
// End Send Broadcast
The receiver part on another class:
textLat = (TextView) findViewById(R.id.lat);
textLong = (TextView) findViewById(R.id.lon);
getValues();
Log.d("IrelandGeoFire",String.valueOf(latDouble));
Log.d("IrelandGeoFire",String.valueOf(lonDouble));
// initialize GoogleMaps
initGMaps();
}
public void getValues() {
// Gathering the lat/long from GPSService
Intent gpsIntent = new Intent(this, GPSService.class);
startService(gpsIntent);
latDouble = gpsIntent.getDoubleExtra("latitude",0 );
lonDouble = gpsIntent.getDoubleExtra("longitude", 0);
}
The log:
01-29 15:03:09.612 6081-6081/xxx D/GeoFire: 0.0
01-29 15:03:09.612 6081-6081/xxx D/GeoFire: 0.0
Anything wrong?
I would prefer use LocalBroadcastManager for this.
UPDATE: Above issue was due to the lack of filtername. Thanks for that. Below is the update code where I try to receive the data with put/get extra, but that is not updated.
On logcat:
01-29 21:33:26.449 17042-17042/xxx D/changed: after 30
01-29 21:33:26.491 17042-17042/xxx D/Receiver: after 30
01-29 21:33:26.491 17042-17042/xxx D/Receiver: null
Sender:
onLocationChanged:
String msg = "Updated Location: " +
Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
Log.d("changed","after 30");
intent.putExtra("xxx", msg);
sendBroadcast(intent);
Receiver:
onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.geofire);
mIntentFilter=new IntentFilter();
mIntentFilter.addAction("GPS_Service");
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"after 30 - Geo"); // I receive it
Log.d(TAG,String.valueOf(test1));// This is null!!
}
};
registerReceiver(mReceiver, mIntentFilter);
textLat = (TextView) findViewById(R.id.lat);
textLong = (TextView) findViewById(R.id.lon);
getValues();
Log.d("GeoFire",String.valueOf(latDouble));
Log.d("GeoFire",String.valueOf(lonDouble));
// initialize GoogleMaps
initGMaps();
}
public void getValues() {
Intent intent = new Intent(this, GPSService.class);
test1 = intent.getStringExtra("xxx");
startService(intent);
}
Upvotes: 0
Views: 290
Reputation: 620
You can try cast your long to string put as string into your intent and cast in the other side, like the following part of your code:
lat = String.valueOf(location.getLatitude());
lon = String.valueOf(location.getLongitude());
Upvotes: 0
Reputation: 222
You should mention broadcast name and put one receiver for that particular name in on receiver method of broadcast you will get passed intent through that you can get lat/long
Upvotes: 1