简体   繁体   English

手机屏幕锁定时 onLocationChanged 不起作用

[英]onLocationChanged not working while phone screen is locked

I've created an app which tracks the user and draws polyline.我创建了一个跟踪用户并绘制折线的应用程序。 In some cellphones when the phone is locked, the onlocationchanged listener stops working and doesn't take the locations where the user passed, so the polylines are not drawn.在某些手机中,当手机被锁定时,onlocationchanged 监听器停止工作并且不会获取用户经过的位置,因此不会绘制折线。

can someone tell me how to resolve my problem?有人可以告诉我如何解决我的问题吗? why when the phone is locked, the location listener stops working?为什么当手机被锁定时,位置监听器停止工作?

here is a my code in fragment:这是我的代码片段:

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);
    }
}

when you lock the phone your activity gets paused, to get location in background you need to use service there are so many resources to get location in background.当您锁定手机时,您的活动会暂停,要在后台获取位置,您需要使用服务,有很多资源可以在后台获取位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM