1

Possible Duplicate:
How to programmatically enable GPS in Android Cupcake

I'm currently writing an app in Android that works with the GPS. At the moment I'm able to work out whether the GPS is enabled. My problem is that I want to enable the GPS on app startup if it is disabled. How can I do this programmaticaly? also, i want to create functions that turn on and off the GPS, i read all the threads on stackoverflow about it however all the functions that i tried i got a "unfortanly your app must stopped" (i didnt forget to add permission)

can someone help me with a working function to enable or disable GPS?

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

at first, i used these functions:

 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);
        }
    }

    private void turnGPSOff(){
        String provider = Settings.Secure.getString(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")); 
            sendBroadcast(poke);
        }
    }

then i tried use:

ENABLE GPS:

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

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

both are not working for me

any one has idea?

Community
  • 1
  • 1
fsdf fsd
  • 1,301
  • 4
  • 13
  • 13
  • Just because you get a force close dialog doesn't mean the method is bad. Did you try debugging that issue or did you give up? Maybe you should post a code example that you tried and what error you received so we can better help you. – Samuel Sep 14 '12 at 20:37
  • thanks for reply, i added the functions that i try use.. i hope someone will help me to solve the problem – fsdf fsd Sep 14 '12 at 20:46
  • 2
    in Android you can't (and shouldn't btw) activate GPS programatically. All you can do is to open Positioning Settings Menu and let user switch it on by himself. – alex Sep 14 '12 at 20:48
  • hi, i take it from here : http://stackoverflow.com/questions/6925832/switch-off-gps-programatically also how can i open the GPS settings window? – fsdf fsd Sep 14 '12 at 20:55
  • Check following several questions: http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker http://stackoverflow.com/questions/10302894/ics-android-enable-gps-programmatically-closed-using-an-alternative-approach http://stackoverflow.com/questions/3878689/how-can-we-turn-on-off-gps-programatically-without-going-on-setting-screen-in-an – Victor Ronin Sep 14 '12 at 20:55
  • thanks all, i tried all however the app stopped when i use the functions suggested in these threads...maybe someone has another idea? – fsdf fsd Sep 14 '12 at 20:58
  • so on one knows a programatically way to enable or disable gps? – fsdf fsd Sep 14 '12 at 21:07
  • I already told you: you **can not** do this. This is because of security reasons - nobody wants to be tracked without explicit permission. Use Jeffrey's answer, that's all you can do. – alex Sep 14 '12 at 21:16
  • that hack to set the GPS on / off only works on older versions of android (i can't recall the exact version). don't use it. – Jeffrey Blattman Sep 14 '12 at 23:19
  • It may be done for rooted devices only. Event that is non-trivial. The application must include WRITE_SECURE_SETTINGS permission and be able to move itself to /system/app directory. Root and BusyBox required but actually that's all. For CM9/10 it works but I'm not so sure for stock rooted ICS/JB. – OGP Feb 11 '13 at 16:29

1 Answers1

7

you cannot programmatically turn GPS on and off. the best you can do is send the user to the settings screen that allows them to do it themselves.

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
     new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
     startActivity(intent);
}

there existed hacks to turn GPS on / off programmatically, but they work only in older versions of Android. even if you could, don't do this. the user may have turned off GPS because they didn't want to allow apps to track them precisely. it's extremely bad form to try and force change their decision.

if you require GPS to be enabled, check for it when your app starts and bail if the user won't enable it.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134