I have a service class which will get the user's location from 9:00 am to 18:00 pm. I am using alarm Manager to trigger the selfStop();
which is in the broadcast receiver like
Alarm Manager:
public void StopTracking() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, StopTime);
calendar.set(Calendar.MINUTE, StopMin);
String stop = "stop";
registerReceiver(stopReceiver, new IntentFilter(stop));
if (PendingIntent.getBroadcast(
this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT) != null) {
Log.i(TAG, "Pending Intent Started");
PendingIntent broadcastIntent = PendingIntent.getBroadcast(
this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
assert alarmManager != null;
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcastIntent);
} else {
Log.i(TAG, "StopTracking: Pending Intent Stoped!");
}
}
protected BroadcastReceiver stopReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "received stop broadcast");
// Stop the service when the notification is tapped
unregisterReceiver(stopReceiver);
stopSelf();
intent = new Intent(context, StopService.class);
context.startService(intent);
}
};
but i am unable to stop the location function in the same service class bellow is my location function
Location Function:
private void requestLocationUpdates() {
request.setInterval(10000);
request.setFastestInterval(5000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
final String path = getString(R.string.firebase_path) + "/" + getString(R.string.transport_id);
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permission == PackageManager.PERMISSION_GRANTED) {
// Request location updates and when an update is
// received, store the location in Firebase
client.requestLocationUpdates(request, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
Location location = locationResult.getLastLocation();
if (location != null) {
ref.setValue(location);
Toast.makeText(TrackerService.this, "Success", Toast.LENGTH_SHORT).show();
String lattitude = String.valueOf(location.getLatitude());
String longitude = String.valueOf(location.getLongitude());
Save_LocationOffline(lattitude, longitude);
}
}
}, null);
}
}
This is my Problem,
The broadcast receiver works and i i have triggered the selfStop();
and OnDestroy
part worked, but location function is still working and not stopped.
Can any one explain what is the problem. Thanks in Advance.
It seems that you are using the google play service for location in order to retrieve the location.
If you check the FusedLocationProviderClient documentation, they said that when you use requestLocationUpdates(LocationRequest, LocationCallback, Looper)
, you should call removeLocationUpdates(LocationCallback)
.
You first need to extract the location callback, for example like this:
class MyService .... {
// Member into your service
private LocationCallback mLocationCallback = new LocationCallback() {...}
private void requestLocationUpdates() {
...
client.requestLocationUpdates(request, mLocationCallback, null)
...
}
// Into you stopReceiver
public void onReceive(Context context, Intent intent) {
...
client.removeLocationUpdate(mLocationCallback )
...
}
}