-3

I have started working on GoogleMap in Android and I want that my application to turn on the GPS automatically as the application get launched and turn off when the application goes close or user get exit from application.

How to get done above task ?

Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
  • 2
    Using Google Play Services you can enable Location Services directly from a dialog rather than sending the user to the settings menu, see here: http://stackoverflow.com/questions/31235564/locationsettingsrequest-dialog-onactivityresult-skipped/31816683#31816683 – Daniel Nugent Oct 07 '15 at 04:52
  • Possible duplicate of [How do I get the current GPS location programmatically in Android?](http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) – Jaimin Modi Oct 14 '15 at 05:26

4 Answers4

3

You can prompt user to turn on GPS like:

private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(
                this.getActivity());
        builder.setMessage(R.string.gps_disabled)
                .setCancelable(false)
                .setTitle(R.string.gps_disabled_title)
                .setPositiveButton(R.string.enable,
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    @SuppressWarnings("unused") final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                startActivity(new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            }
                        })
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                dialog.cancel();

                            }
                        });
        final AlertDialog alert = builder.create();
        alert.show();
    }
Jas
  • 3,207
  • 2
  • 15
  • 45
2

GPS can't really be turned on automatically by an Android app. It requires user's authentication to do so.

Best you can do is have a popup dialog requesting the user to allow access to the location service.

Nick Hargreaves
  • 436
  • 2
  • 7
1

No it't impossible, and inappropriate. You can't just manage the users phone without his authority.

From Play Store:

"Cerberus automatically enables GPS if it is off when you try to localize your device (only on Android < 2.3.3) and you can protect it from unauthorized uninstalling - more info in the app configuration."

You can do something like this:

startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
0

Full solution is here.

AndroidMAnifest.xml

Probably you need folowing permissions

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

MainActivity code

public class MainActivity extends AppCompatActivity {

Context context;

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //... some code to init Activity and etc...

        context = this;

        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
        boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if(!statusOfGPS) // Before show message to turn on GPS be sure it is turned off.
        {
            buildAlertMessageNoGps();
        }
    }



private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(
                this.getActivity());
        builder.setMessage(R.string.gps_disabled)
                .setCancelable(false)
                .setTitle(R.string.gps_disabled_title)
                .setPositiveButton(R.string.enable,
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    @SuppressWarnings("unused") final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                startActivity(new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            }
                        })
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                dialog.cancel();

                            }
                        });
        final AlertDialog alert = builder.create();
        alert.show();
    }
NoWar
  • 36,338
  • 80
  • 323
  • 498