Get GPS start / stop events using addGpsStatusListener

In the application, I would like to launch a Service that can receive two notifications from GPS: GPS_EVENT_STARTED and GPS_EVENT_STOPPED.

For this, I made the following code:

package com.test;

import android.app.Service;
import android.content.Context;
import android.content.Intent;

import android.location.GpsStatus;
import android.location.LocationManager;
import android.os.IBinder;
import android.util.Log;

public class TestNotification extends Service {

    private LocationManager mLm;
    private MyListener mMyListener;

    private class MyListener implements GpsStatus.Listener {
        @Override
        public void onGpsStatusChanged(int event) {
            Log.i("MyGps", "Event");

            switch(event) {
            case GpsStatus.GPS_EVENT_STARTED:
                Log.i("MyGps", "Started");
                break;
            case GpsStatus.GPS_EVENT_STOPPED:
                Log.i("MyGps", "Stopped");
                break;
            }
        }    
    }

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

    @Override
    public void onCreate() {
        mLm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        mMyListener = new MyListener();
        mLm.addGpsStatusListener( mMyListener );
    }
}

I also set the following permission in the manifest: ACCESS_FINE_LOCATION I do not want to use requestLocationUpdates because it does GPS work every time and send events when corrections change.

He tests it on the emulator and on a real device (Eclair), but it does not work, the Service never receives notifications.

I read the following chain in detail ( here ), but no solution works. I am sending it on a separate issue because I only want to get information about the Start and Stop GPS, and not the fix.

- ? , , , Activity?

.

+3
1

AndroidManifest.xml:

<service android:enabled="true" android:name="com.test.TestNotification" />
0

Source: https://habr.com/ru/post/1765576/


All Articles