I am aware there are a number of similar questions floating around but could not find a suitable answer in my searches.
My App has an activity which relies on the GPS location to function. The app will crash if it is run immediately after start up due to null pointer exceptions etc where the location is null. I am aware these are my programming errors and I should handle the exceptions, but the activity is still useless until a GPS fix is found.
When the app crashes I get the "Searching for GPS" icon, and if this is left for long enough a GPS location is found and the app can be run with no issue.
I have created another activity to act as a home screen which will enable the button to link to the above activity when a GPS location is found. The problem is that it doesn't seem to find or be looking for a GPS signal. By this i mean that no "Searching for GPS" icon appears and the button is never enabled. The code for this home activity is below, have I missed something?
My activity implements LocationListener, and the code below is in the Activity "onCreate" method. "startExplore" starts my GPS reliant activity.
exploreButton = (Button) findViewById(R.id.button1);
exploreButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startExplore();
}
});
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
exploreButton.setEnabled(false);
exploreButton.setText("Finding location . . .");
}
This is my "onLocationChanged" method. As i said above, unlike my other activity I do not get the "Searching for GPS" icon and the location does not appear to be found despite this being the same method used in my other Activity. So is this the correct technique for waiting until a location is found, and can anyone identify any mistakes?
public void onLocationChanged(Location location) {
exploreButton.setEnabled(true);
exploreButton.setText("Found signal");
}