1. method 1
Private void toggleGPS (){
Intent gpsIntent = new Intent ();
GpsIntent. setClassName ("com. android. settings", "com. android. settings. widget. SettingsAppWidgetProvider ");
GpsIntent. addCategory ("android. intent. category. ALTERNATIVE ");
GpsIntent. setData (Uri. parse ("custom: 3 "));
Try {
PendingIntent. getBroadcast (StartActivity. this, 0, gpsIntent, 0). send ();
} Catch (CanceledException e ){
E. printStackTrace ();
}
}
2. method 2
Private void openGPSSettings (){
// Obtain the current GPS status (ON or OFF)
Boolean gpsEnabled = Settings. Secure. isLocationProviderEnabled (getContentResolver (), LocationManager. GPS_PROVIDER );
If (gpsEnabled)
{
// Disable GPS
Settings. Secure. setLocationProviderEnabled (getContentResolver (), LocationManager. GPS_PROVIDER, false );
}
Else
{
// Enable GPS www.2cto.com
Settings. Secure. setLocationProviderEnabled (getContentResolver (), LocationManager. GPS_PROVIDER, true );
}
3. Method 3 (manual setting)
LocationManager alm = (LocationManager) StartActivity. this. getSystemService (Context. LOCATION_SERVICE );
If (alm. isProviderEnabled (android. location. LocationManager. GPS_PROVIDER ))
{
Toast. makeText (this, "GPS module normal", Toast. LENGTH_SHORT). show ();
}
Toast. makeText (this, "Turn on GPS! ", Toast. LENGTH_SHORT). show ();
Intent intent = new Intent (Settings. ACTION_SECURITY_SETTINGS );
StartActivityForResult (intent, 0); // return to the get interface after the setting is complete.
First, the second type requires additional permissions.
<! -- Allow the program to read or write data to system settings -->
<Uses-permission android: name = "android. permission. WRITE_SETTINGS"> </uses-permission>
<Uses-permission android: name = "android. permission. WRITE_SECURE_SETTINGS"/>
Author: beihai1212