如何停止服务 Class 中的 onLocationResult()?

How to stop onLocationResult() in Service Class?

我有一项服务 class 可以获取用户从 9:00 上午到 18:00 下午的位置。我正在使用警报管理器触发广播接收器中的 selfStop();,如

警报管理器:

 public void StopTracking() {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, StopTime);
        calendar.set(Calendar.MINUTE, StopMin);


        String stop = "stop";
        registerReceiver(stopReceiver, new IntentFilter(stop));

        if (PendingIntent.getBroadcast(
                this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT) != null) {
            Log.i(TAG, "Pending Intent Started");
            PendingIntent broadcastIntent = PendingIntent.getBroadcast(
                    this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            assert alarmManager != null;
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcastIntent);
        } else {
            Log.i(TAG, "StopTracking: Pending Intent Stoped!");
        }
    }

    protected BroadcastReceiver stopReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "received stop broadcast");
            // Stop the service when the notification is tapped

            unregisterReceiver(stopReceiver);
            stopSelf();

            intent = new Intent(context, StopService.class);
            context.startService(intent);
        }
    };

但我无法在同一服务中停止定位功能class 下面是我的定位函数

定位函数:

private void requestLocationUpdates() {

    request.setInterval(10000);
    request.setFastestInterval(5000);
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


    final String path = getString(R.string.firebase_path) + "/" + getString(R.string.transport_id);
    int permission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION);
    if (permission == PackageManager.PERMISSION_GRANTED) {
        // Request location updates and when an update is
        // received, store the location in Firebase
        client.requestLocationUpdates(request, new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
                Location location = locationResult.getLastLocation();

                if (location != null) {
                    ref.setValue(location);
                    Toast.makeText(TrackerService.this, "Success", Toast.LENGTH_SHORT).show();
                    String lattitude = String.valueOf(location.getLatitude());
                    String longitude = String.valueOf(location.getLongitude());

                    Save_LocationOffline(lattitude, longitude);
                }
            }
        }, null);
    }
}

这是我的问题,

广播接收器工作并且我触发了 selfStop();OnDestroy 部分工作,但是定位功能仍在工作并且没有停止.

谁能解释一下是什么问题。 提前致谢。

您似乎正在使用 google 位置播放服务来检索位置。

如果你检查FusedLocationProviderClient documentation,他们说当你使用requestLocationUpdates(LocationRequest, LocationCallback, Looper)时,你应该调用removeLocationUpdates(LocationCallback)

您首先需要提取位置回调,例如这样:

 class MyService .... {
    // Member into your service
    private LocationCallback mLocationCallback = new LocationCallback() {...}

    private void requestLocationUpdates() {
        ...
         client.requestLocationUpdates(request, mLocationCallback, null)
        ...
    }

    // Into you stopReceiver
    public void onReceive(Context context, Intent intent) {
        ...
        client.removeLocationUpdate(mLocationCallback )
        ...
    }

}