0

Go to Settings -> Location Services-> Check on access to my location, I wanted to do this programatically autoEnable ,I tried this below code but not working

Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");      
intent.putExtra("enabled", true);
sendBroadcast(intent);
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);     

          } 
}
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
sushma1008
  • 125
  • 5
  • 14

1 Answers1

0

Consider API Version //Enabled


 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);

//Disabled


Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);

Set Permisstion in Android Manifest


<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

More Details Please Visit

Community
  • 1
  • 1
Abdulla Sirajudeen
  • 1,269
  • 1
  • 16
  • 29