title: turn on off GPS android
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1051649/how-to-programmatically-enable-gps-in-android-cupcake">How to programmatically enable GPS in Android Cupcake</a>  </p>
  </blockquote>
  
  
  
  <p>I'm currently writing an app in Android that works with the GPS. At the moment I'm able to work out whether the GPS is enabled. My problem is that I want to enable the GPS on app startup if it is disabled. How can I do this programmaticaly?
  also, i want to create functions that turn on and off the GPS, i read all the threads on stackoverflow about it however all the functions that i tried i got a "unfortanly your app must stopped" (i didnt forget to add permission) </p>
  
  <p>can someone help me with a working function to enable or disable GPS?</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_SETTINGS" /&gt;
  </code></pre>
  
  <p>at first, i used these functions:</p>
  
  <pre><code> private void turnGPSOn(){
          String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  
          if(!provider.contains("gps")){ //if gps is disabled
              final Intent poke = new Intent();
              poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
              poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
              poke.setData(Uri.parse("3")); 
              sendBroadcast(poke);
          }
      }
  
      private void turnGPSOff(){
          String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  
          if(provider.contains("gps")){ //if gps is enabled
              final Intent poke = new Intent();
              poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
              poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
              poke.setData(Uri.parse("3")); 
              sendBroadcast(poke);
          }
      }
  </code></pre>
  
  <p>then i tried use:</p>
  
  <pre><code>ENABLE GPS:
  
  Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
  intent.putExtra("enabled", true);
  sendBroadcast(intent);
  DISABLE GPS:
  
  Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
  intent.putExtra("enabled", false);
  sendBroadcast(intent);
  </code></pre>
  
  <p>both are not working for me</p>
  
  <p>any one has idea?</p>