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

I used the above code in my main java class but it did not work. Is there any other way?

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Mohendra
  • 161
  • 1
  • 7

3 Answers3

1

This question is discussed many times. As I know, you can't do it automatically from Android 4.0.3. If you are using Android 2+, did you add following line to AndroidManifest.

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

There is a different approach. That is use Google Settings API.

com.google.android.gms:play-services:8.1.0

Check Google Samples

Check this question as well.

Community
  • 1
  • 1
Isuru
  • 3,818
  • 13
  • 49
  • 64
0
LocationManager mlocationManager;
Boolean isGPSEnabled;

private static boolean checkGPSConnection(){
    if(mLocationManager == null)
        mLocationManager = (LocationManager) mcontext.getSystemService(Context.LOCATION_SERVICE);

    isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    return isGPSEnabled;
}

Use checkGPSConnection() method where you want to check GPS is available or not.

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
vimal raj
  • 295
  • 1
  • 13
-1

In some devices you can't turn on/off GPS programmatically, but there another great approach which will work in all devices(nonrooted/rooted). Just show dialog which open GPS settings where user can turn gps himself.

 public void statusCheck()
            {
                final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

                if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
                    buildAlertMessageNoGps();

                }


            }
             private void buildAlertMessageNoGps() {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
                           .setCancelable(false)
                           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                               public void onClick(final DialogInterface dialog,  final int id) {
                                   startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                               }
                           })
                           .setNegativeButton("No", new DialogInterface.OnClickListener() {
                               public void onClick(final DialogInterface dialog, final int id) {
                                    dialog.cancel();
                               }
                           });
                    final AlertDialog alert = builder.create();
                    alert.show();

                }

Or try use this code to turn it automatically, but in some OS its will not work.

public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);


    }
}
// automatic turn off the gps
public void turnGPSOff()
{
    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);
    }
}
Ognev Zair
  • 1,626
  • 1
  • 13
  • 17