Sunil Meena
Sunil Meena

Reputation: 45

Android location service stop working when app destroyed or goes in background

I am building a location based application and I have created a service which works in background. When app goes in background or destroyed and in my service class if I play audio in loop it works perfectly.

Audio is playing continue even I destroy app and for getting user location I am using FusedLocationProviderClient API and it also works from service but only when app is in foreground when app user goes to other app or destroys app this it's not getting location but media player is working fine in same service.

My Service Class

public class ServiceClass extends Service {

    private MediaPlayer mediaplayer;
    private FusedLocationProviderClient fusedLocationProviderClient;
    private LocationRequest locationRequest;
    private Context mContext;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        //Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
        mContext = this;
        mediaplayer = MediaPlayer.create(this,Settings.System.DEFAULT_RINGTONE_URI);
        mediaplayer.setLooping(true);
        mediaplayer.start();
        getLocation();

        return START_STICKY;
    }

    public void getLocation() {

        Toast.makeText(mContext, "Engine X started YO", Toast.LENGTH_SHORT).show();

        fusedLocationProviderClient = new FusedLocationProviderClient(this);
        locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setFastestInterval(1000);
        locationRequest.setInterval(1000);

        fusedLocationProviderClient.requestLocationUpdates(locationRequest, new LocationCallback(){

            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                 Log.d("X_loc_BAck", ""+locationResult.getLastLocation().getAccuracy());
                Toast.makeText(mContext, ""+locationResult.getLastLocation().getLatitude(), Toast.LENGTH_SHORT).show();



            }
        },getMainLooper());

    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        mediaplayer.stop();
        //Toast.makeText(this, "Service Desttroyed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        mediaplayer.stop();
        Intent i = new Intent(this, ServiceStopped.class);
        sendBroadcast(i);
        //Toast.makeText(this, "Service Task Removed", Toast.LENGTH_SHORT).show();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Upvotes: 0

Views: 586

Answers (1)

Merve Sahin
Merve Sahin

Reputation: 1048

According to the documentation:

This method is suited for the foreground use cases. For background use cases, the PendingIntent version of the method is recommended, see requestLocationUpdates(LocationRequest, PendingIntent).

Take a look at this sample project with PendingIntent.

Upvotes: 1

Related Questions