B
B
BitNeBolt2020-05-17 16:25:06
Android
BitNeBolt, 2020-05-17 16:25:06

How to start the service in the background?

To get gps coordinates, I decided to create a service that, in theory, should work in the background, when the screen is off or when the application is minimized. Here is the service:

Here

public class GpsService extends Service {

    private LocationListener listener;
    private LocationManager locationManager;
    private int counter = 0;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Intent i = new Intent("location_update");
                i.putExtra("speed", String.valueOf(location.getSpeed()));
                i.putExtra("counter", String.valueOf(counter));
                sendBroadcast(i);

                Toast.makeText(getApplicationContext(), String.valueOf(counter), Toast.LENGTH_SHORT).show();

                counter++;
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {
            }
        };

        locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

        //noinspection MissingPermission
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0.1f, listener);

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(locationManager != null){
            //noinspection MissingPermission
            locationManager.removeUpdates(listener);
        }
    }
}



Here's what's added to the manifest:
Here

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

        <service
            android:name=".GpsService"
            android:enabled="true"
            android:foregroundServiceType="location"
            android:exported="true"></service>



But if you close the application or the screen, the counter will not increase and the Toast messages will disappear.

When I tried to launch foregroundService "on the forehead", the program crashed when reopened.

What am I doing wrong?
But if you minimize the program or

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2020-05-17
@BitNeBolt

That's right, the system beats such services.
Read documentation https://developer.android.com/training/best-background
You can run foreground service,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question