Fixed issue with not receiving location data.

master
Tomasz Półgrabia 2016-08-26 22:59:05 +02:00
parent 840f2025c4
commit d7bc9707d3
2 changed files with 4 additions and 32 deletions

View File

@ -33,7 +33,6 @@ public class HomeFragment extends Fragment implements LocationListener {
private boolean locationEnabled; private boolean locationEnabled;
private LocationManager locationService; private LocationManager locationService;
private String locationProvider; private String locationProvider;
private HomeFragment homeFrag;
private boolean locationServicesActivated = false; private boolean locationServicesActivated = false;
public HomeFragment() { public HomeFragment() {
@ -64,20 +63,13 @@ public class HomeFragment extends Fragment implements LocationListener {
return inflater.inflate(R.layout.fragment_home, container, false); return inflater.inflate(R.layout.fragment_home, container, false);
} }
public void setLocationData(double lat, double lng) {
TextView locationInfo = (TextView) getActivity().findViewById(R.id.locationInfo);
locationInfo.setText("Location: (" + lat + "," + lng + ")");
}
@Override @Override
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
Log.i(CLASS_TAG, "Location provider changed: " + location); Log.i(CLASS_TAG, "Location provider changed: " + location);
double lat = location.getLatitude(); double lat = location.getLatitude();
double lng = location.getLongitude(); double lng = location.getLongitude();
HomeFragment homeFrag = (HomeFragment) getActivity() TextView locationInfo = (TextView) getActivity().findViewById(R.id.locationInfo);
.getSupportFragmentManager() locationInfo.setText("Location: (" + lat + "," + lng + ")");
.findFragmentById(R.id.home);
homeFrag.setLocationData(lat, lng);
} }
@Override @Override
@ -99,12 +91,11 @@ public class HomeFragment extends Fragment implements LocationListener {
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
homeFrag = (HomeFragment) getChildFragmentManager().findFragmentById(R.id.home_frag);
if (locationProvider != null) { if (locationProvider != null) {
locationService.requestLocationUpdates(locationProvider, locationService.requestLocationUpdates(locationProvider,
MIN_TIME, MIN_TIME,
MIN_DISTANCE, MIN_DISTANCE,
homeFrag); this);
locationServicesActivated = true; locationServicesActivated = true;
Toast.makeText(getActivity(), "Location resumed", Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), "Location resumed", Toast.LENGTH_LONG).show();
} }
@ -114,7 +105,7 @@ public class HomeFragment extends Fragment implements LocationListener {
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
if (locationServicesActivated) { if (locationServicesActivated) {
locationService.removeUpdates(homeFrag); locationService.removeUpdates(this);
} }
} }

View File

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="pl.tpolgrabia.urbanexplorer.MainActivity">
<Toolbar android:layout_width="match_parent"
android:layout_height="wrap_content"
android:title="Urban explorer">
</Toolbar>
</LinearLayout>