Get GPS start/stop events with addGpsStatusListener

最后都变了- 提交于 2019-12-21 19:53:44

问题


In an application I would like to start a Service that can receive two notification from GPS: GPS_EVENT_STARTED and GPS_EVENT_STOPPED.

To do so I've done 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 have set the following permission in manifest: ACCESS_FINE_LOCATION I don't want to use requestLocationUpdates because it makes the GPS working everytime and send events when fixes change.

It test it on emulator and on real device (Eclair) but it doesn't work, the Service never receive any notification.

I've read in detail the following thread (here) but no solution work. I send it in a separate question because I only want to be informed of GPS start and stop, not fixes.

Do you have any advice on this? May be it is because I try to do it in a Service rather that in an Activity?

Thanks for reading.


回答1:


try to put this into AndroidManifest.xml file:

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


来源:https://stackoverflow.com/questions/3749539/get-gps-start-stop-events-with-addgpsstatuslistener

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!