Set gps to auto-enable for android

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.