-2

Is it possible to turn on LocationProviders(GPS Provider/Network Providers) on Device programatically, if it is turned off?

i can turning on gps without problem in android 2.2 and 2.3, but in android 4.0 or 4.1 when i turn on gps with this code.gps icon appear in notifications,but i can not get current location in my program

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

what i do now?i need it emergency

1 Answers1

1

GeoBits posted the right link of the explaination why GPS should (and can) not be turned on programmaticaly.

Insteed just open the settings menu of the device and let do the user the job. Therefor you have to use an Intent like this:

    Intent i = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(i);

It's also possible to start this Activity for result, to check if the user has enabled GPS or not in onActivityResult(). This can be done like this:

    public boolean checkForGpsProvider() {
        LocationManager locationManager = (LocationManager)
                getSystemService(Context.LOCATION_SERVICE);

        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • i tested this in android 4.0.3 and 4.1.2. gps icon show in notification bar when i turn on gps in code,but i can not get location i need this for my program who test it and it work fine? – hamid khakzad Jul 23 '13 at 16:17
  • To turn on GPS and to get the actual location are different stories. It would be better if you open up a new question with your actual problem – Steve Benett Jul 23 '13 at 16:56