0

I've read the Google doc and this post notably the interesting @bendaf's answer, about managing Location settings, and everything works well.

Anyway one problem remains if the user first decide to not use its position and then decide later to activate it, the application is not trigged by this action so I don't know that I can request for periodic updates.

What did I miss here?

Community
  • 1
  • 1
fralbo
  • 2,534
  • 4
  • 41
  • 73

3 Answers3

0

You can provide an AlertDialog every time user launches an app so that user can enable the location. But this will be a bit annoying because every time you have to deny to same thing.

Alternatively, you can use Preferences so that user can enable/disable the location

hittsss
  • 76
  • 5
  • Maybe I didn't clearly explained the problem. Consider the user starting the application. I asked him to enable the GPS and he answers no. Later while still using the app, he realises he wants to use his location then go to the settings and enable the GPS. My application is not informed about this new setting. Except if I create a thread to pull for `isGPSEnabled()`. – fralbo Nov 07 '16 at 10:34
  • Yes you can do that. or also you can can create a loop as: while(isRunning){ boolean b=isGPSEnabled(); if(b) { // Your code } } on the same thread. – hittsss Nov 07 '16 at 10:51
0

There is another better way to do it. Use LocationListener. It is for the sole purpose that you want to achieve.

public class XYZ Activity implements LocationListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10,this);
}


@Override
public void onLocationChanged(Location location) {
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
}

I think this is the right thing that you are looking for. It's the event way. Happy Coding :)

hittsss
  • 76
  • 5
0

@fralbo Although this thread is a tiny bit old, I just wanted to provide a solution for you, as I recently had to do something similar myself.

I would recommend implementing a BroadcastReceiver into your App, with the intent filter PROVIDERS_CHANGED..

This will trigger every time the Location/GPS Provider state changes - and you can use an if statement inside of the BroadcastReceiver's onReceive method, to determine whether or not your required conditions are met, and proceed accordingly.. For example, inside the onReceive method of the BroadcastReceiver, you can determine whether the PROVIDERS_CHANGED event has made the GPS become available (and to what degree) - and if it now meets the needs of your App, you can then call whichever Method within your App that is responsible for starting the required calls to the GPS engine, etc.

Here's an example of what the code might look like:

public class LocationReceiver extends BroadcastReceiver {
    private final static String TAG = "[ LocationReceiver ]:";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "PROVIDERS_CHANGED has been detected - Firing onReceive");

        //  Retrieve the LocationManager
        LocationManager locationManager = 
            (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //  Provide boolean references for Location availability types
        boolean isGpsEnabled;
        boolean isNetworkEnabled;

        //  Provide values to retrieve the Location availability
        isGpsEnabled = 
            locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = 
            locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        Log.i(TAG, "Detected - GPS: "+ isGpsEnabled + "NET: "+ isNetworkEnabled);

        //  If (for example), the GPS is ENABLED, start one of your Activities, etc.
        if (isGpsEnabled) {

            Intent startYourActivity = 
                new Intent(context.getApplicationContext(), YourActivity.class);

            context.startActivity(startYourActivity);

        }
    }
}


I hope this helps! Better late than never :)

Happy coding!

Studio2bDesigns
  • 578
  • 5
  • 12