title: How to check if GPS is Disabled Android
body: <p>I have two files MainActivity.java and HomeFragment.java in the MainActivity calls a function from HomeFragment that asks the user to turn on the location services on their phone. The problem is even when the user has the location feature already turned on the function is called anyway. Is there a way I can make it so only when the location feature is off that the function from the HomeFragment is launched.</p>
  
  <p><strong>HomeFragment.java</strong> </p>
  
  <pre><code>public static void displayPromptForEnablingGPS(
          final Activity activity)
  {
      final AlertDialog.Builder builder =
              new AlertDialog.Builder(activity);
      final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
      final String message = "Enable either GPS or any other location"
              + " service to find current location.  Click OK to go to"
              + " location services settings to let you do so.";
  
  
      builder.setMessage(message)
              .setPositiveButton("OK",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface d, int id) {
                              activity.startActivity(new Intent(action));
                              d.dismiss();
                          }
                      });
  
      builder.create().show();
  }
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>public void showMainView() {
      HomeFragment.displayPromptForEnablingGPS(this);
  }
  </code></pre>
  
  <p>Thank you :)</p>