Anlık konum takibi ile ilgili firebase ile realtime olarak çalışan bir ufak uygulama yazmaya çalışıyorum. Uygulamanın çalışmasında herhangi bir problem yok fakat başlattığım servisi durduramıyorum ve arkaplanda anlık olarak yine firebase'e veri göndermeye devam ediyor.
Servis kısmının kodlarını aşağıda paylaşıyorum.
Çok farklı yöntemler denedim fakat bir türlü yapamadım Mobil konusunda da biraz yeniyim muhtemelen gözümden kaçan birkaç yer var

public class TrackerService extends Service {
User user;
private static final String TAG = TrackerService.class.getSimpleName();
LocationRequest request;
@Override
public IBinder onBind(Intent intent) {return null;}
@Override
public void onCreate() {
super.onCreate();
buildNotification();
loginToFirebase();
user = SharedPrefManager.getInstance(this).getUser();
}
private void buildNotification() {
String stop = "stop";
registerReceiver(stopReceiver, new IntentFilter(stop));
PendingIntent broadcastIntent = PendingIntent.getBroadcast(
this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT);
// Create the persistent notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.notification_text))
.setOngoing(true)
.setContentIntent(broadcastIntent)
.setSmallIcon(R.drawable.ic_logo);
startForeground(1, builder.build());
}
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();
}
};
private void loginToFirebase() {
// Authenticate with Firebase, and request location updates
String email = getString(R.string.firebase_email);
String password = getString(R.string.firebase_password);
FirebaseAuth.getInstance().signInWithEmailAndPassword(
email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>(){
@Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "firebase auth success");
requestLocationUpdates();
} else {
Log.d(TAG, "firebase auth failed");
}
}
});
}
private void requestLocationUpdates() {
request = new LocationRequest();
request.setInterval(1000);
request.setFastestInterval(5000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(this);
final String path = getString(R.string.firebase_path) + "/" + user.getId();
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) {
Map<String, Object> myList = new HashMap<>();
myList.put("UserName", user.getAd()+" "+user.getSoyad());
myList.put("accuracy", location.getAccuracy());
myList.put("altitude", location.getAltitude());
myList.put("bearing", location.getBearing());
myList.put("elapsedRealtimeNanos", location.getElapsedRealtimeNanos());
myList.put("fromMockProvider", location.isFromMockProvider());
myList.put("latitude", location.getLatitude());
myList.put("longitude", location.getLongitude());
myList.put("provider", location.getProvider());
myList.put("speed", location.getSpeed());
myList.put("time", location.getTime());
ref.setValue(myList);
}
}
}, null);
}
}
}Servisi Activity'de şu şekilde kullanıyorum. startService(new Intent(getApplicationContext(), TrackerService.class));
stopService(new Intent(getApplicationContext(), TrackerService.class));Start etmede problem yok stop etmede Servis tarafında onDestroy kullanmama rağmen arkada çalışan intenti sonlandıramıyorum.
Edit:
requestLocationUpdates kısmında çalışan request ile ilgili olduğunu tahmin etmekteyim.
Yardımlarınızı bekliyorum