0

我创建了一个跟踪用户并绘制折线的应用程序。在某些手机中,当手机被锁定时,onlocationchanged 监听器会停止工作,并且不会获取用户经过的位置,因此不会绘制折线。

有人可以告诉我如何解决我的问题吗?为什么当手机被锁定时,位置监听器停止工作?

这是我的代码片段:

package ir.fragments;

public class OneFragment extends Fragment implements
    PermissionsListener,
    MapboxMap.OnMapClickListener,
    OnMapReadyCallback,
    LocationListener, SensorEventListener, StepListener {

    private List<LatLng> points = new ArrayList<>();
    private LocationComponent locationComponent;
    private PermissionsManager permissionsManager;
    private LocationEngine locationEngine;
    private MapboxMap mapboxMap_global;


    public OneFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cx = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Mapbox.getInstance(getActivity(), getString(R.string.mapbox_access_token));
        view = inflater.inflate(R.layout.fragment_one, container, false);
        my_configMap(savedInstanceState);

        return view;
    }


    //-----------------------------------------//
    //-----------START NAVIGATION--------------//
    //-----------------------------------------//


    @SuppressLint("MissingPermission")
    @Override
    public void onMapReady(@NonNull MapboxMap mapboxMap) {

        mapboxMap_global = mapboxMap;

        obj_myMAPBOX = new MY_MAPBOX_CLASS(mapboxMap_global, "waiting", "1");
        mapboxMap_global.addOnMapClickListener(this);
        mapboxMap_global.setStyle(Style.MAPBOX_STREETS, style -> {
            if (PermissionsManager.areLocationPermissionsGranted(getActivity())) {
                locationComponent = mapboxMap.getLocationComponent();
                locationComponent.activateLocationComponent(getActivity(), style);
                locationComponent.setRenderMode(RenderMode.GPS);
                locationComponent.setLocationComponentEnabled(true);
                locationComponent.setCameraMode(CameraMode.TRACKING_COMPASS);

                this.map_style = style;

                //--------------****peida kardane location avaliye****--------////
                LocationEngine locationEngine1 = LocationEngineProvider.getBestLocationEngine(getActivity());
                locationEngine1.getLastLocation(new LocationEngineCallback<LocationEngineResult>() {
                    @Override
                    public void onSuccess(LocationEngineResult result) {

                        //Toast.makeText(getActivity(),"Place Your First Option Code",Toast.LENGTH_SHORT).show();
                        if (fill_My_Primary_Location(result)) {

                           //get_activity_info();


                            CameraPosition position = new CameraPosition.Builder()
                                    .target(userCurrentLocation_latLng)
                                    .zoom(15)
                                    .tilt(20)
                                    .build();
                            mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 500);


                        }


                    }

                    @Override
                    public void onFailure(@NonNull Exception exception) {

                    }
                });

                //--------------****end of peida kardane location avaliye****--------////



                lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
                //lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                this.onLocationChanged(null);

            } else {
                permissionsManager = new PermissionsManager(this);
                permissionsManager.requestLocationPermissions(getActivity());
            }

        });
    }


    @Override
    public void onLocationChanged(Location location) {
        //Toast.makeText(getActivity(),"im changing"+location,Toast.LENGTH_LONG).show();
        if (location != null)
        {
            Location s1 = new Location("");
            s1.setLatitude(stations.get(race_station).getLatitude());
            s1.setLongitude(stations.get(race_station).getLongitude());

            drawPolyline(s1);
        }

    }




    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    @Override
    public void onSensorChanged(SensorEvent event) {

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
    }

    @Override
    public void step(long timeNs) {
        if (status.equals("start")) {
            numSteps++;
            F1_TextView_1.setText(numSteps + "");
        }
    }

    //-----------------------------------------//
    //-----------END NAVIGATION--------------//
    //-----------------------------------------//

    @Override
    public void onExplanationNeeded(List<String> permissionsToExplain) {

    }

    @Override
    public void onPermissionResult(boolean granted) {

    }

    @Override
    public boolean onMapClick(@NonNull LatLng point) {
        return false;
    }


    public void my_configMap(Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this);


    }




    // Add the mapView's own lifecycle methods to the activity's lifecycle methods
    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
4

1 回答 1

-1

当您锁定手机时,您的活动会暂停,要在后台获取位置,您需要使用服务,有很多资源可以在后台获取位置。

于 2019-10-08T12:48:19.430 回答