【问题标题】:How to stop onLocationResult() in Service Class?如何在服务类中停止 onLocationResult()?
【发布时间】:2018-04-07 06:49:30
【问题描述】:

我有一个服务类,它将从上午 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);
        }
    };

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

定位功能:

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 部分工作,但定位功能仍在工作并且没有停止

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

【问题讨论】:

    标签: android locationlistener google-location-services


    【解决方案1】:

    您似乎正在使用 google play 服务获取位置以检索位置。

    如果你检查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 )
            ...
        }
    
    }
    

    【讨论】:

    • 是的,我正在使用 google play 服务在运行时进行实时资产跟踪!
    • 好的,我不需要任何回调,因为我不会向用户显示我需要删除请求更新就是它
    • 回调是接收位置。这是 request/removeLocationUpdate 中的相同回调
    • 这就是我所做的。我将来自 client.updateLocationUpdate 的回调放在服务内部,因为您将需要此回调来调用 removeUpdateLocation
    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 2018-01-02
    • 2017-03-17
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多