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