0

I am new to a service. I want to turn on my GPS sensor and get the lat and long coordinate of GPS. I am using Android M, I provided the permission for my app. However, I cannot enable and get coordinate. Could you help me?

code :

//Enable gps
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")) {
    new Handler(getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            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);
        }
    });
}
Akash Patel
  • 2,757
  • 1
  • 22
  • 30
John
  • 2,838
  • 7
  • 36
  • 65

2 Answers2

0

You can not switch on or switch off GPS programatically in android. Could you clearify your question more, whether you are getting trouble in switching GPS on and off or in getting lat long` if the problem is switching it on and off then you can guide the user to setting screen and ask them to switch on user when ever you need , here is code

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

`

Navneet Kumar
  • 273
  • 1
  • 11
  • Yes. First. My GPS is turn off now. I want to use programmatically to turn on it. Then. I want to get the coordinate value from the sensor. Thanks – John Mar 04 '17 at 11:59
  • The one which you used is an exploit , it might not work for your device – Navneet Kumar Mar 04 '17 at 12:20
  • Thanks. SO could you give me the way to get coordinate with simple way? Assume that GPS is on now – John Mar 04 '17 at 15:04
0

first you need run time permission for location in marshmallow android version

add your main activity

in oncreate() method

 if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Log.d("PLAYGROUND", "Permission is not granted, requesting");
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 123);

    } else {
        Log.d("PLAYGROUND", "Permission is granted");
    }

below oncreate() method

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 123) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.d("PLAYGROUND", "Permission has been granted");

        } else {
            Log.d("PLAYGROUND", "Permission has been denied or request cancelled");
            finish();
        }
    }
}

and then as usual your stuff

Akash pasupathi
  • 304
  • 1
  • 14