I am new to Xamarin development. I have created one background service which is used to track user location. It's working properly till the time application is running/open. As application is closed/destroyed, my background service stops working. I have already spent so much time to find out the reason behind this issue. But bad luck.
[Service]
public class LocationTrackingService : Service, GoogleApiClient.IConnectionCallbacks,
GoogleApiClient.IOnConnectionFailedListener, Android.Gms.Location.ILocationListener
{
protected const string TAG = "Attendance Frag";
protected const int REQUEST_CHECK_SETTINGS = 0x1;
public const long UPDATE_INTERVAL_IN_MILLISECONDS = 20 * 1000;
public const long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
protected const string KEY_REQUESTING_LOCATION_UPDATES = "requesting-location-updates";
protected const string KEY_LOCATION = "location";
protected const string KEY_LAST_UPDATED_TIME_STRING = "last-updated-time-string";
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
protected LocationSettingsRequest mLocationSettingsRequest;
public override void OnCreate()
{
base.OnCreate();
BuildGoogleApiClient();
CreateLocationRequest();
Toast.MakeText(this, "OnCreate", ToastLength.Short).Show();
}
protected void BuildGoogleApiClient()
{
Log.Info("AttendanceFrag", "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(LocationServices.API)
.Build();
}
protected void CreateLocationRequest()
{
mLocationRequest = new LocationRequest();
mLocationRequest.SetInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.SetFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
}
protected void BuildLocationSettingsRequest()
{
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.AddLocationRequest(mLocationRequest);
builder.SetAlwaysShow(true);
mLocationSettingsRequest = builder.Build();
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
base.OnStartCommand(intent, flags, startId);
if (mGoogleApiClient != null && !mGoogleApiClient.IsConnected)
mGoogleApiClient.Connect();
return StartCommandResult.Sticky;
}
protected async Task StartLocationUpdates()
{
await LocationServices.FusedLocationApi.RequestLocationUpdates(
mGoogleApiClient,
mLocationRequest,
this
);
}
protected async Task StopLocationUpdates()
{
await LocationServices.FusedLocationApi.RemoveLocationUpdates(
mGoogleApiClient,
this
);
}
public async void OnConnected(Bundle connectionHint)
{
Log.Info(TAG, "Connected to GoogleApiClient");
await StartLocationUpdates();
}
public void OnConnectionSuspended(int cause)
{
Log.Info(TAG, "Connection suspended");
}
public void OnConnectionFailed(Android.Gms.Common.ConnectionResult result)
{
Log.Info(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.ErrorCode);
}
public void OnLocationChanged(Location location)
{
Toast.MakeText(this, "OnLocationChanged", ToastLength.Short).Show();
}
public override async void OnDestroy()
{
base.OnDestroy();
Toast.MakeText(this, "OnDestroy", ToastLength.Short).Show();
if (mGoogleApiClient != null
&& mGoogleApiClient.IsConnected)
{
await StopLocationUpdates();
}
//Start GeoLocation Tracking Service again
Intent i = new Intent(this, typeof(RestartServiceReceiver));
i.SetAction(RestartServiceReceiver.START_TRACKING);
SendBroadcast(i);
}
}
Please, help me
Start your service in a private process like this
[Service(Name = "com.xamarin.TimestampService",
Process=":timestampservice_process",
Exported=true)]
more info on running service in process here.
you can also register it as foreground service in that way they won't be stop by android and will show up in notification bar
Sorry for the late answer but I think it can help others. I would like to make something clear for you that you are writing this service as a background service. Background service can not run for long. There are some limitation of background services in Android after Android 8.0 onwards. Android automatically kills background service of an app after some time.
See this https://developer.android.com/about/versions/oreo/background
If you want to run a service for a long time then make the service Foreground Service. Please follow https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services for detailed knowledge of foreground service in Xamarin Forms.
Related
My problem is i can get the location in background but once app is killed it stopped updating.
i am storing the latitude and longitude into firebase once it's fetching from getlastlocation method. and foreground and background mode it's updating fine. but when i close like clearing all the background apps then it is stop updating.
public class MyBackgroundLocationService extends Service {
private static final String TAG = MyBackgroundLocationService.class.getSimpleName();
private FusedLocationProviderClient mLocationClient;
private LocationCallback mLocationCallback;
public MyBackgroundLocationService() {
}
#Override
public void onCreate() {
super.onCreate();
mLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
mLocationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
Log.d(TAG, "onLocationResult: location error");
return;
}
List<Location> locations = locationResult.getLocations();
LocationResultHelper helper = new LocationResultHelper(getApplicationContext(), locations);
helper.showNotification();
helper.saveLocationResults();
Toast.makeText(getApplicationContext(), "Location received: " + locations.size(), Toast.LENGTH_SHORT).show();
}
};
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: called");
startForeground(1001, getNotification());
getLocationUpdates();
return START_STICKY;
}
private Notification getNotification() {
NotificationCompat.Builder notificationBuilder = null;
notificationBuilder = new NotificationCompat.Builder(getApplicationContext(),
App.CHANNEL_ID)
.setContentTitle("Location Notification")
.setContentText("Location service is running in the background.")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
return notificationBuilder.build();
}
private void getLocationUpdates() {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(4000);
locationRequest.setMaxWaitTime(15 * 1000);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
stopSelf();
return;
}
mLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.myLooper());
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: called");
stopForeground(true);
mLocationClient.removeLocationUpdates(mLocationCallback);
}
}
so i am using foreground service to update location in background and it will show in notification those updates. once app is killed notification also disappeared. please please please anyone help me.
if you need more clear about question i will explain. please someone understand my pain
I'm developing an android application to track user location in background and keep updating it to firebase database as location changes the app is working fine when the application is opened but when i close it , firebase stop setting new values , it will set the new values directly after I open the application again
this is my android service code :
public class LocationServiceFB extends Service implements LocationListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{
private Location mLastLocation;
DatabaseReference instructors;
GeoFire geofire;
String UserId;
SharedPreferences pref;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
private static int UPDATE_INTERVAL = 5000;
private static int DISPLACEMENT = 10;
private static int FATEST_INTERVAL = 3000;
private LocationManager mLocationManager;
#Nullable
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not Yet Implemented");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
instructors = FirebaseDatabase.getInstance().getReference("OnlineInstructors");
geofire = new GeoFire(instructors);
pref = this.getSharedPreferences("LoginTrack", Context.MODE_PRIVATE);
UserId = pref.getString("firebasekey","");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
instructors.child(UserId).removeValue();
}
#Override
public void onConnected(#Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
}
#Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
}
#Override
public void onLocationChanged(Location location) {
mLastLocation = location;
geofire.setLocation(UserId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
#Override
public void onComplete(String key, DatabaseError error) {
Log.d("ERROR" , "INSTRUCTOR LOCATION SENT TO DATABASE" );
}
});
}
public void uploadLocation(){
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d("EER","np permissions");
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
final double latitude = mLastLocation.getLatitude();
final double longitude = mLastLocation.getLongitude();
geofire.setLocation(UserId, new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {
#Override
public void onComplete(String key, DatabaseError error) {
Log.d("ERROR" , "INSTRUCTOR LOCATION SENT TO DATABASE" );
}
});
}
}
Android may kill your app's process in favor of more important apps. Android may also stop your process from networking when it's not longer visible in the foreground. This is to prevent poorly behaved apps from consuming too many resources.
If your apps needs to continue networking when the user is no longer using it, you'll have to start a foreground service, which also requires that you show a notification that indicates to the user that your app is still running.
You are using a bound service for upadating your position to the server.
Your service gets killed along with app.
Use an unbound service. Unbound service keeps running in the background even when the app is killed. So, this will work fine even in the background with unbound service.
I started Service from main activity like;-
Intent intent = new Intent(this, MyLocationService.class);
startService(intent);
MyLocationService class looks like:-
public class MyLocationService extends Service implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = MyLocationService.class.getSimpleName();
public static Location mCurrentLocation;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
#Override
public void onCreate() {
Log.e(TAG, "onCreate: ");
initiateGooglePlayService();
}
public void initiateGooglePlayService() {
Log.e(TAG, "initiateGooglePlayService: ");
if (isGooglePlayServicesAvailable()) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(10.0f);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand: ");
return super.onStartCommand(intent, flags, startId);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind: ");
return null;
}
private boolean isGooglePlayServicesAvailable() {
Log.e(TAG, "isGooglePlayServicesAvailable: ");
int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getApplicationContext());
return ConnectionResult.SUCCESS == status;
}
#Override
public void onConnected(Bundle bundle) {
Log.e(TAG, "onConnected: ");
startLocationUpdates();
}
#Override
public void onConnectionSuspended(int i) {
Log.e(TAG, "onConnectionSuspended: ");
}
protected void startLocationUpdates() {
Log.e(TAG, "startLocationUpdates: ");
try {
PendingResult<Status> pendingResult;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
} catch (IllegalStateException ignored) {
}
}
#Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location.getLongitude());
if (mGoogleApiClient.isConnected()) {
mCurrentLocation = location;
Intent intent = new Intent("GPSLocationUpdates");
intent.putExtra("location", location);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Toast.makeText(this, "location", Toast.LENGTH_LONG).show();
}
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed: ");
}
}
I never stop the service anywhere.
But still I am unable to get the control of onLocationChange().
My motive is that, I need continuous location to do some background operation. It worked in lollipop sometimes. But it is not working in Marshmallow and nougat and even kitkat also. I searched but could not get the proper idea. Please let me know, where i am going wrong. Any suggestion accepted.
I am using following dependency for location;-
compile 'com.google.android.gms:play-services-location:9.6.1'
You can use the HyperTrack SDK to get the location updates in the background. Get more insight about the reliability of SDK here in different conditions.
First Setup the SDK
After setting up the SDK you just need to set the Callback to receive the location updates.
HyperTrack.setCallback(new HyperTrackEventCallback() {
#Override
public void onEvent ( #NonNull final HyperTrackEvent event){
switch (event.getEventType()) {
case HyperTrackEvent.EventType.LOCATION_CHANGED_EVENT:
Log.d(TAG, "onEvent: Location Changed");
HyperTrackLocation hyperTrackLocation = event.getLocation();
LatLng latLng = hyperTrackLocation.getLatLng();
updateCurrentLocationMarker(event.getLocation());
break;
}
}
}
(Disclaimer: I work at HyperTrack.)
/**Check out my Github post i did the same for Taxi clone**/
Link ---> https://github.com/yash786agg/GPS/
Note: Remember to remove or comment the below mentioned code if you want the latitude
and latitude even when the application is in background.
if(networkUtilObj != null)
{
networkUtilObj.disconnectGoogleApiClient();
}
I am developing an app that should send gps data to a server once every 10 minutes. The problem is that when I kill the app when the GPS is ON, the GPS icon disappear from the status bar (while the GPS is still ON) and the data are not sent anymore. If I do the same with the google maps app the icon doesn't disappear.
Can someone explain me why this is happening?
here is the code that I use
AlarmManager
/**
* ALARM_MANAGER_GPS_TIME_INTERVAL = 10 min
*/
PendingIntent mPendingIntent = PendingIntent
.getService(
context,
PENDING_INTENT_REQUEST_CODE_FOR_GPS,
new Intent(context, GpsDataService.class),
PendingIntent.FLAG_IMMUTABLE);
AlarmManager mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mAlarmManager.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
ALARM_MANAGER_GPS_TIME_INTERVAL,
mPendingIntent);
Service
public class GpsDataService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
/**
* Provides the entry point to Google Play services.
*/
protected GoogleApiClient mGoogleApiClient;
/**
* Stores parameters for requests to the FusedLocationProviderApi.
*/
protected LocationRequest mLocationRequest;
/**
* Represents a geographical location.
*/
protected Location mCurrentLocation;
private String mPayLoad;
private GpsPojoBuilder mGpsPojoBuilder;
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(mGoogleApiClient == null)
buildGoogleApiClient();
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected())
startLocationUpdates();
return Service.START_NOT_STICKY;
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
/**
* UPDATE_INTERVAL_IN_MILLISECONDS = 4 sec
* FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 2 sec
*/
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest
.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS)
.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
#Override
public void onConnected(#Nullable Bundle bundle) {
startLocationUpdates();
}
#Override
public void onConnectionSuspended(int i) {
// The connection to Google Play services was lost for some reason. Call connect() to
// attempt to re-establish the connection.
mGoogleApiClient.connect();
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
}
/**
* Requests location updates from the FusedLocationApi.
*/
protected void startLocationUpdates() {
checkForPermission();
}
private void checkForPermission() {
Location location;
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//permission denied
} else {
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location != null)
handleLocation(location);
else
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}else {
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(location != null)
handleLocation(location);
else
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
#Override
public void onLocationChanged(Location location) {
handleLocation(location);
}
private void handleLocation(Location location){
mCurrentLocation = location;
Toast.makeText(this, "location foound: " + location.toString(), Toast.LENGTH_SHORT).show();
Thread thread = new Thread() {
#Override
public void run() {
try {
mGpsPojoBuilder = new GpsPojoBuilder(getApplicationContext());
mPayLoad = mGpsPojoBuilder
.getCheckInJson(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
sendJSON(mPayLoad, CHECKINS_URL);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(mGoogleApiClient.isConnected()) {
stopLocationUpdates();
mGoogleApiClient.disconnect();
}
}
}
};
thread.start();
}
public void sendJSON(String payLoadData, String url_suffix) {
HttpConnectionCreator mHttpConncectionClass = new HttpConnectionCreator(payLoadData, url_suffix);
mHttpConncectionClass.connect();
}
/**
* Removes location updates from the FusedLocationApi.
*/
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
if (mGoogleApiClient.isConnected()) {
stopLocationUpdates();
mGoogleApiClient.disconnect();
}
super.onDestroy();
}
}
According to android.developer, if you want to keep your services alive, you need to use START_STICKY.
int START_STICKY
Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service.
inside the following function:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(mGoogleApiClient == null)
buildGoogleApiClient();
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected())
startLocationUpdates();
return Service.START_NOT_STICKY;
}
change to this:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(mGoogleApiClient == null)
buildGoogleApiClient();
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected())
startLocationUpdates();
return Service.START_STICKY;
}
I am trying to create a route tracking app. it need to track location even if the app is in background. so i created a service and add code to this service. Following are my code. but there is one problem. I start the service from my main activity.
public void startTracking(View view) {
startService(new Intent(MainActivity.this, LocationIntentService.class));
}
public void stopTracking(View view) {
stopService(new Intent(MainActivity.this, LocationIntentService.class));
}
It start the service and locations are inserted to a local db. But i cant stop these service. When i stop service using above code it still track the location. How can i stop location update.
public class LocationIntentService extends IntentService implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = LocationIntentService.class.getSimpleName();
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;
private static int DISPLACEMENT = 10;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
DBAdapter dbAdapter;
public LocationIntentService() {
super("LocationIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.e(TAG, " ***** Service on handled");
if (isGooglePlayServicesAvailable()) {
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
#Override
public void onConnected(Bundle bundle) {
Log.e(TAG, " ***** Service on connected");
startLocationUpdates();
openDB();
}
#Override
public void onConnectionSuspended(int i) {
Log.e(TAG, " ***** Service on suspended");
mGoogleApiClient.connect();
}
#Override
public void onLocationChanged(Location location) {
Log.e(TAG, "Location changed");
mLastLocation = location;
String latitude = String.valueOf(mLastLocation.getLatitude());
String longitude = String.valueOf(mLastLocation.getLongitude());
Log.e(TAG, " ##### Got new location"+ latitude+ longitude);
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
String timestamp = today.format("%Y-%m-%d %H:%M:%S");
dbAdapter.insertRow(latitude, longitude, timestamp);
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
+ connectionResult.getErrorCode());
}
#Override
public void onDestroy() {
Log.e(TAG, "Service is Destroying...");
super.onDestroy();
if (mGoogleApiClient.isConnected()) {
stopLocationUpdates();
mGoogleApiClient.disconnect();
}
closeDB();
}
protected void stopLocationUpdates() {
Log.d(TAG, "Location update stoping...");
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
protected void startLocationUpdates() {
Log.d(TAG, "Location update starting...");
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
private void openDB() {
dbAdapter = new DBAdapter(this);
dbAdapter.open();
}
private void closeDB() {
dbAdapter = new DBAdapter(this);
dbAdapter.close();
}
protected void createLocationRequest() {
Log.e(TAG, " ***** Creating location request");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
Log.e(TAG, " ***** Update google play service ");
return false;
}
}
}
The reason that it's not working for you is that you are using an IntentService, so calling stopService() will not cause onDestroy() to be called, presumably because it was already called after onHandleIntent() has completed. There is no need to ever call stopService() on an IntentService see here.
It looks like you should probably just use Service instead of IntentService. That way, when you call stopService(), it would call onDestroy() and unregister for location updates, as you expect.
The only other change you would need to make would be to override onStartCommand() instead of onHandleIntent().
You would have your class extend Service instead of IntentService, and then move your code to register for location updates to onStartCommand:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, " ***** Service on start command");
if (isGooglePlayServicesAvailable()) {
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
return Service.START_STICKY;
}
This way you can still call startService() and stopService(), and it should work as you are expecting.
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
call stopLocationUpdates() method in stopService()
When you stop your services. Then called this line in LocationIntentService.class.
locationManager.removeUpdates(this);