I followed this to Get Location Coordinates For Every 10 Seconds
So Now Here I want to Change the Activity to Service So that I will start it as Background Service to Get Updates on Location for Every 10 Seconds...
But Here I modified UpdateUI()
method
private void updateUI() {
mLatitudeTextView.setText(String.format("%s: %f", mLatitudeLabel,
mCurrentLocation.getLatitude()));
mLongitudeTextView.setText(String.format("%s: %f", mLongitudeLabel,
mCurrentLocation.getLongitude()));
mLastUpdateTimeTextView.setText(String.format("%s: %s", mLastUpdateTimeLabel,
mLastUpdateTime));
}
to
private void updateUI() {
try{
mlatitude = String.valueOf(mCurrentLocation.getLatitude());
mlongitude = String.valueOf(mCurrentLocation.getLongitude());
}
catch(Exception e){
Toast.makeText(this,"Location Not Found",Toast.LENGTH_LONG).show();
}
}
And I am Toasting those values at onStartCommand()
and I have Given On start Command like this
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
buildGoogleApiClient();
mRequestingLocationUpdates = true;
updateUI();
return mStartMode;
}
But here it runs only for 1st time and it's not Displaying Values and it's not getting updates or not running for every 10 seconds.
However in Activity its working Fine I am getting Location Updates for every 10 seconds...
Can Anyone suggest how to Give the same in Service... so that I will get Updates
Update 1: To Run a Service for every 10 seconds I have added this...
private final Integer TEN_SECONDS = 10000;
public void sendSendLocation() {
new Handler().postDelayed(new Runnable() {
public void run () {
updateUI();
new Handler().postDelayed(this, TEN_SECONDS);
}
},TEN_SECONDS);
}
But Here Location Displays Null... Can anyone suggest me...
Update 2:
Actually, I need to add updateValuesFromBundle(savedInstanceState)
in OnStartCommand
if I add it... the Service will work. But hence its a service I am Unable to Give Bundle savedInstanceState
...
private void updateValuesFromBundle(Bundle savedInstanceState) {
Log.i(TAG, "Updating values from bundle");
if (savedInstanceState != null) {
// Update the value of mRequestingLocationUpdates from the Bundle, and make sure that
// the Start Updates and Stop Updates buttons are correctly enabled or disabled.
if (savedInstanceState.keySet().contains(REQUESTING_LOCATION_UPDATES_KEY)) {
mRequestingLocationUpdates = savedInstanceState.getBoolean(
REQUESTING_LOCATION_UPDATES_KEY);
setButtonsEnabledState();
}
// Update the value of mCurrentLocation from the Bundle and update the UI to show the
// correct latitude and longitude.
if (savedInstanceState.keySet().contains(LOCATION_KEY)) {
// Since LOCATION_KEY was found in the Bundle, we can be sure that mCurrentLocation
// is not null.
mCurrentLocation = savedInstanceState.getParcelable(LOCATION_KEY);
}
// Update the value of mLastUpdateTime from the Bundle and update the UI.
if (savedInstanceState.keySet().contains(LAST_UPDATED_TIME_STRING_KEY)) {
mLastUpdateTime = savedInstanceState.getString(LAST_UPDATED_TIME_STRING_KEY);
}
updateUI();
}
}
So I am Running UpdateUI()
Directly at OnStartCommand()
in service
Anyone Suggest me On this kind?