1. The first method
Private voidTogglegps () {Intent gpsintent=NewIntent (); 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. The second method
Private voidopengpssettings () {//Get GPS status now (open or closed)Boolean gpsenabled =Settings.Secure.isLocationProviderEnabled (Getcontentresolver (), locationmanager.gps_provider); if(gpsenabled) {//Turn off GPSSettings.Secure.setLocationProviderEnabled (Getcontentresolver (), Locationmanager.gps_provider,false ); } Else { //Turn on GPSSettings.Secure.setLocationProviderEnabled (Getcontentresolver (), Locationmanager.gps_provider,true); }
3. Third method (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,"Please open gps! ", Toast.length_short). Show (); Intent Intent=NewIntent (settings.action_security_settings); Startactivityforresult (Intent,0);//This is the return to the Get interface after Setup is complete
The first and second kind need to add permission
<!--allows programs to read or write system settings--<uses-permission android:name="Android.permission.WRITE_SETTINGS " ></uses-permission> <uses-permission android:name="android.permission.WRITE_ Secure_settings"/>
Description
The first: Some of the phones are invalid
The second type: since Level8
The third type: it is commonly used in this
http://blog.csdn.net/beihai1212/article/details/7692317
Android Settings GPS Auto-Open