MuayThai
MuayThai

Reputation: 451

Allowing User to Set GPS

I want to give user dialog to set GPS on/off. Can this be done using the new SettingAPI? If so, how: I saw the code below, but not sure if it works for Android 2.3 devices via compatibility library or not? What versions of Android are supported. In particular does user need to be connected to net to use this service for GPS or will it work with settings on device directly no need for internet connection? Also does the dialog show just GPS settings or can user access other parts of settings? I'm trying to restrict to just GPS Settings.

LocationSettingsStates locationSettingsStates = locationSettingsResult.getLocationSettingsStates();

    if (!locationSettingsStates.isGpsPresent() || !locationSettingsStates.isGpsUsable()) {

 Status status = locationSettingsResult.getStatus();
 if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
 try {
 status.startResolutionForResult(StilActivity.this, REQUEST_CHECK_SETTINGS);
 } catch (IntentSender.SendIntentException th) {
 Log.e(TAG, "Error opening settings activity.", th);
          }
      }
 }

Upvotes: 0

Views: 112

Answers (1)

Vaibhav Barad
Vaibhav Barad

Reputation: 625

Just call the function and then

   public void start() {
        mlocManager = (LocationManager) 
                getSystemService(Context.LOCATION_SERVICE);

        if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(getApplicationContext(), "Response ==>" + "GPS is on",
                    Toast.LENGTH_LONG).show();
        } else {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    MainActivity.this);
            alertDialogBuilder
                    .setMessage("GPS is disabled in your device. Enable it?")
                    .setCancelable(false)
                    .setPositiveButton("Enable GPS",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    Intent callGPSSettingIntent = new Intent(
                                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    MainActivity.this.startActivity(callGPSSettingIntent);
                                }
                            });
            alertDialogBuilder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = alertDialogBuilder.create();
            alert.show();

        }
    }

And just as a slight suggestion you could call the startActivityforResult() and the once the user comes back from the location page you can do your further execution.

Upvotes: 1

Related Questions