无法停止位置监听器android?

时间:2016-10-14 05:12:34

标签: android location android-library

我正在使用此SmartLocation库。我可以使用代码停止活动中的服务。

SmartLocation.with(this).location().stop();

但是当我在服务中使用这个库时,我能够启动,但它不会停止,直到我关闭应用程序。还有一点需要注意的是,当我使用stopService()时。

OnDestroy()也被称为

其余的服务代码在这里

public class locationService extends Service implements OnLocationUpdatedListener,   OnActivityUpdatedListener
 {
    private LocationGooglePlayServicesProvider provider;

    NotificationCompat.Builder builder;
    NotificationManager notificationManager;

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

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        provider = new LocationGooglePlayServicesProvider();
        provider.setCheckLocationSettings(true);

        SmartLocation smartLocation = new SmartLocation.Builder(getApplicationContext()).logging(true).build();

        smartLocation.location(provider).continuous().start(this);
        smartLocation.activity().start(this);

        return START_NOT_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public boolean stopService(Intent name) {
        return super.stopService(name);
    }

    @Override
    public void onDestroy() {
        stop();
    }

    @Override
    public void onLocationUpdated(Location location) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
    }

    @Override
    public void onActivityUpdated(DetectedActivity detectedActivity) {
    }

    public void stop()
    {
        SmartLocation.with(this).location().stop();
        stopForeground(true);
    }
}

1 个答案:

答案 0 :(得分:0)

我正在使用活动识别,因为我正在使用服务停止,它没有工作。所以我将smartlocation实例声明为全局,然后我将其停止为preventDefault

以下是我修改后的代码

smartLocation.location().stop();
相关问题