title: How to? Listen for Location Setting being turned ON (Android App)
body: <p>So I've spent the past few weeks working on my Android App and looking into the best way of implementing what I need to do, but still can't quite get it right.. Any/all help is greatly appreciated, as I am still getting the hang of things..</p>
  
  <h2>Task:</h2>
  
  <p>(Assuming the "Location"/GPS Setting is currently off), I need to have my App constantly be listening for the "Location" Setting to be turned ON.. At this point, simply start an Activity.</p>
  
  <h2>Ideas:</h2>
  
  <p>These are all the different ways I think it could possibly work:</p>
  
  <ul>
  <li><p>LocationListener using "onProviderEnabled"</p></li>
  <li><p>GpsStatusListener using "onGpsStatusChanged" with "GPS_EVENT_STARTED"</p></li>
  <li><p>GpsProvider requiresSatellite (to determine if it starts), or somehow use the GpsProvider's "AVAILABLE" Constant/int</p></li>
  <li><p>SettingInjectorService using "ACTION_SERVICE_INTENT" (and/or) "ACTION_INJECTED_SETTING_CHANGED" with "onGetEnabled" or "isEnabled"</p></li>
  <li><p>Settings.Secure using "LOCATION_MODE" != "LOCATION_MODE_OFF"</p></li>
  <li><p>a ContentObserver/ContentResolver</p></li>
  <li><p>Intent getAction (...)</p></li>
  <li><p>An "if/else" of some kind</p></li>
  </ul>
  
  <h2>Questions:</h2>
  
  <p>Any kind of advice or answers for any of the following questions are very much appreciated..</p>
  
  <ul>
  <li><p>Which of the Ideas above would be the best way of accomplishing the Task? The simpler the better, but most importantly it needs to be listening at all times, and respond instantly when the Location Setting is turned On.</p></li>
  <li><p>For whichever one of the Ideas  above works best, how would I implement it? (For example, would I need a BroadcastListener? or a Service? and how would it all piece together?</p></li>
  </ul>
  
  <p>I truly appreciate any advice or help that you can provide me with.. I'm still getting the hang of all this, but confident enough to do it, and eager to   publish my first App.. So thank you, it means a lot and will greatly help me along.<br>
  <br>
  <br></p>
  
  <p>EDIT:<br>
  <br>
  OK So here's what I've got so far...
  <br>
  Heres my Receiver:
  <br>
  <br>
  MyReceiver.Java<br>
  </p>
  
  <pre><code>public class MyReceiver extends BroadcastReceiver {
  
      private final static String TAG = "LocationProviderChanged";
  
      boolean isGpsEnabled;
      boolean isNetworkEnabled;
  
  
  
      public MyReceiver() {
      // EMPTY
  
      // MyReceiver Close Bracket
      }
  
  
  
      // START OF onReceive
      @Override
      public void onReceive(Context context, Intent intent) {
  
  
          // PRIMARY RECEIVER
          if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
  
              Log.i(TAG, "Location Providers Changed");
  
              LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
              isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
              isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  
              Toast.makeText(context, "GPS Enabled: " + isGpsEnabled + " Network Location Enabled: " + isNetworkEnabled, Toast.LENGTH_LONG).show();
  
              // START DIALOG ACTIVITY
              if (isGpsEnabled || isNetworkEnabled) {
                  Intent runDialogActivity = new Intent(context, DialogActivity.class);
                  context.startActivity(runDialogActivity);
  
              }
  
          }
  
  
  
          // BOOT COMPLETED (REPLICA OF PRIMARY RECEIVER CODE FOR WHEN BOOT_COMPLETED)
          if (intent.getAction().matches("android.intent.action.BOOT_COMPLETED")) {
  
              Log.i(TAG, "Location Providers Changed Boot");
  
              LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
              isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
              isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  
              Toast.makeText(context, "GPS Enabled Boot: " + isGpsEnabled + " Network Location Enabled Boot: " + isNetworkEnabled, Toast.LENGTH_LONG).show();
  
              // START DIALOG ACTIVITY
              if (isGpsEnabled || isNetworkEnabled) {
                  Intent runDialogActivity = new Intent(context, DialogActivity.class);
                  context.startActivity(runDialogActivity);
  
              }
  
          }
  
  
  
      // onReceive CLOSE BRACKET
      }
  
  
  
  // CLOSE OF FULL CLASS
  }
  </code></pre>
  
  <p>And heres what the Manifest looks like:</p>
  
  <p>Manifest.XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ender.projects.receivertest"&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:fullBackupContent="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme"&gt;
  
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity android:name=".DialogActivity"&gt;
          &lt;/activity&gt;
  
          &lt;receiver
              android:name=".MyReceiver"
              android:enabled="true"
              android:exported="true"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.location.PROVIDERS_CHANGED" /&gt;
  
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Aside from these files, I have the following files:<br>
  <code>MainActivity.Java</code> and <code>DialogActivity.Java</code>, both with Layout files,<br>
  <code>activity_main.xml</code> and <code>activity_dialog.xml</code> to match with them.<br>
  <br>
  So if I understand correctly, when the user downloads the App and opens it, my <code>MainActivity.Java</code> and corresponding layout will launch. But I am only going to use this simply as a preferences screen. So once they open the App for the first time, the Broadcast Receiver should automatically start listening for the LOCATION setting to be turned on, correct? I also want the Broadcast Listener to remain listening, even after it receives the initial Broadcast (so that my <code>onReceive</code> still fires if they turn the LOCATION setting off, and then later they turn it ON again..<br>
  <br>
  So (A) How does my code look thus far?<br>
  and (B) To accomplish what I just described, what would need to be added?<br>
  and (C) Running it throws this error when I turn the LOCATION setting ON.<br>
  ..How can I fix it?:<br>
  <code>java.lang.RuntimeException: Unable to start receiver com.bryce.projects.servicesthreadsetc.MyReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?</code><br>
  <br>
  <br>
  Thanks again for all the help!<br></p>