Not working fragment & location requests but working switching location

on and off.
master
Tomasz Półgrabia 2016-08-26 22:48:04 +02:00
parent 42b7b9e76a
commit 840f2025c4
3 changed files with 95 additions and 72 deletions

View File

@ -14,80 +14,14 @@ public class MainActivity extends ActionBarActivity {
private static final int LOCATION_SETTINGS_REQUEST_ID = 1; private static final int LOCATION_SETTINGS_REQUEST_ID = 1;
private static final String CLASS_TAG = MainActivity.class.getSimpleName(); private static final String CLASS_TAG = MainActivity.class.getSimpleName();
private static final long MIN_TIME = 60000;
private static final float MIN_DISTANCE = 100;
private boolean gpsLocationEnabled;
private boolean networkLocationEnabled;
private boolean locationEnabled;
private LocationManager locationService;
private String locationProvider;
private HomeFragment homeFrag;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.navbar); // Toolbar toolbar = (Toolbar) findViewById(R.id.navbar);
setSupportActionBar(toolbar); // setSupportActionBar(toolbar);
locationService = (LocationManager) getSystemService(LOCATION_SERVICE);
checkLocationSourceAvailability();
if (!locationEnabled) {
Intent locationSettingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(locationSettingsIntent, LOCATION_SETTINGS_REQUEST_ID);
}
}
@Override
protected void onResume() {
super.onResume();
homeFrag = (HomeFragment) getSupportFragmentManager().findFragmentById(R.id.home);
locationService.requestLocationUpdates(locationProvider,
MIN_TIME,
MIN_DISTANCE,
homeFrag);
}
@Override
protected void onPause() {
super.onPause();
locationService.removeUpdates(homeFrag);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case LOCATION_SETTINGS_REQUEST_ID:
checkLocationSourceAvailability();
if (!locationEnabled) {
// sadly, nothing to do except from notifing user that program is not enable working
Toast.makeText(this, "Sorry location services are not working." +
" Program cannot work properly - check location settings to allow program working correctly",
Toast.LENGTH_LONG);
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
private void checkLocationSourceAvailability() {
gpsLocationEnabled = locationService.isProviderEnabled(LocationManager.GPS_PROVIDER);
networkLocationEnabled = locationService.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
locationEnabled = gpsLocationEnabled || networkLocationEnabled;
if (gpsLocationEnabled) {
locationProvider = LocationManager.GPS_PROVIDER;
return;
}
if (networkLocationEnabled) {
locationProvider = LocationManager.NETWORK_PROVIDER;
return;
}
} }
@Override @Override

View File

@ -1,9 +1,12 @@
package pl.tpolgrabia.urbanexplorer.fragments; package pl.tpolgrabia.urbanexplorer.fragments;
import android.content.Intent;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -11,19 +14,49 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.R;
import static android.content.Context.LOCATION_SERVICE;
/** /**
* A simple {@link Fragment} subclass. * A simple {@link Fragment} subclass.
*/ */
public class HomeFragment extends Fragment implements LocationListener { public class HomeFragment extends Fragment implements LocationListener {
private static final String CLASS_TAG = HomeFragment.class.getSimpleName(); private static final String CLASS_TAG = HomeFragment.class.getSimpleName();
private static final long MIN_TIME = 60000;
private static final float MIN_DISTANCE = 100;
private static final int LOCATION_SETTINGS_REQUEST_ID = 1;
private boolean gpsLocationEnabled;
private boolean networkLocationEnabled;
private boolean locationEnabled;
private LocationManager locationService;
private String locationProvider;
private HomeFragment homeFrag;
private boolean locationServicesActivated = false;
public HomeFragment() { public HomeFragment() {
// Required empty public constructor // Required empty public constructor
} }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
checkLocationSourceAvailability();
if (!locationEnabled) {
Intent locationSettingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(locationSettingsIntent, LOCATION_SETTINGS_REQUEST_ID);
return;
}
Toast.makeText(getActivity(), "Created", Toast.LENGTH_LONG).show();
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@ -61,4 +94,60 @@ public class HomeFragment extends Fragment implements LocationListener {
public void onProviderDisabled(String provider) { public void onProviderDisabled(String provider) {
Log.i(CLASS_TAG, "Provider " + provider + " disabled"); Log.i(CLASS_TAG, "Provider " + provider + " disabled");
} }
@Override
public void onResume() {
super.onResume();
homeFrag = (HomeFragment) getChildFragmentManager().findFragmentById(R.id.home_frag);
if (locationProvider != null) {
locationService.requestLocationUpdates(locationProvider,
MIN_TIME,
MIN_DISTANCE,
homeFrag);
locationServicesActivated = true;
Toast.makeText(getActivity(), "Location resumed", Toast.LENGTH_LONG).show();
}
}
@Override
public void onPause() {
super.onPause();
if (locationServicesActivated) {
locationService.removeUpdates(homeFrag);
}
}
private void checkLocationSourceAvailability() {
gpsLocationEnabled = locationService.isProviderEnabled(LocationManager.GPS_PROVIDER);
networkLocationEnabled = locationService.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
locationEnabled = gpsLocationEnabled || networkLocationEnabled;
if (gpsLocationEnabled) {
locationProvider = LocationManager.GPS_PROVIDER;
return;
}
if (networkLocationEnabled) {
locationProvider = LocationManager.NETWORK_PROVIDER;
return;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case LOCATION_SETTINGS_REQUEST_ID:
checkLocationSourceAvailability();
if (!locationEnabled) {
// sadly, nothing to do except from notifing user that program is not enable working
Toast.makeText(getActivity(), "Sorry location services are not working." +
" Program cannot work properly - check location settings to allow program working correctly",
Toast.LENGTH_LONG).show();
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
} }

View File

@ -10,11 +10,11 @@
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="pl.tpolgrabia.urbanexplorer.MainActivity"> tools:context="pl.tpolgrabia.urbanexplorer.MainActivity">
<Toolbar android:id="@+id/navbar" <!--<Toolbar android:id="@+id/navbar"-->
android:layout_width="match_parent" <!--android:layout_width="match_parent"-->
android:layout_height="wrap_content"> <!--android:layout_height="wrap_content">-->
</Toolbar> <!--</Toolbar>-->
<fragment <fragment
android:layout_width="match_parent" android:layout_width="match_parent"