1

I was trying to make a test app so I can play around with the LocationManager but I can't even get passed the new stupid permissions request at runtime. What am I doing wrong and why did they make this so confusing/complicated?

public class MainActivity extends AppCompatActivity {

    private static final int LOCATION_GROUP_PERMISSION_REQUEST = 1;
    private boolean locationPermissionGranted = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getLocationPermission();

        if(locationPermissionGranted) {
            LocationManager locationManager =
                    (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            String provider = LocationManager.GPS_PROVIDER;
            try {
                Location location = locationManager.getLastKnownLocation(provider);
                updateTextView(location);
            } catch(SecurityException e) {
                Log.e("PERMISSION_DENIED", e.getMessage());
            }
        }

        //registerReceiver(gpsReciever, new IntentFilter("android.location.PROVIDERS_CHANGED"));
    }

    private void updateTextView(Location location) {
        Double latitude = location.getLatitude();
        Double longitude = location.getLongitude();
        TextView view = (TextView) findViewById(R.id.textView);
        view.setText(getString(R.string.lat_long_string, latitude, longitude));
    }

    private void getLocationPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission_group.LOCATION},
                        LOCATION_GROUP_PERMISSION_REQUEST);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        // Make sure it's our original READ_CONTACTS request
        if (requestCode == LOCATION_GROUP_PERMISSION_REQUEST) {
            if (grantResults.length == 1 &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                locationPermissionGranted = true;
            }
        }
    }
}
Brad T
  • 1,523
  • 2
  • 12
  • 14
  • I think it would work if you would stop checking the boolean variable is true – 7geeky Sep 03 '16 at 00:34
  • When I remove the boolean check my location var ends up being null at runtime. The device I'm testing this on shows my Location is enabled, so I'm a bit stumped. – Brad T Sep 03 '16 at 00:45

1 Answers1

7

What's the proper way to request permission group Location at runtime in Marshmallow?

You don't. You request permissions at runtime, not permission groups. Remove everything from your code that refers to permission groups (e.g., Manifest.permission_group.LOCATION). Replace it with code that refers to permissions (e.g., Manifest.permission.ACCESS_FINE_LOCATION).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    so, what is the purpose of permission groups if they cannot be used to request a set of permissions, e.g. SMS permissions? – Antonio Apr 11 '17 at 00:54
  • 2
    @Antonio: Permission groups are for display purposes only AFAIK. – CommonsWare Apr 11 '17 at 11:33
  • Thanks for you answer, @CommonsWare. What kind of display purposes? Where are those permissions groups displayed? – Antonio Apr 11 '17 at 13:21
  • @Antonio: In Settings. In the runtime permission dialogs. On Android 5.1 and older, or for apps with `targetSdkVersion` lower than 23 on Android 6.0+, when the app is installed by sideloading or similar mechanisms. – CommonsWare Apr 11 '17 at 13:24
  • Thanks for the clarification! Hope they can add them for permissions request in the future – Antonio Apr 11 '17 at 14:58
  • Does Accessing Read_contacts gives write_contacts as well? – Cyph3rCod3r May 08 '18 at 15:55
  • @Dr.aNdRO: It shouldn't, AFAIK. – CommonsWare May 08 '18 at 16:01
  • makes no sence to me, purpose of groups should be > avoids spamming the user with a lot of permission requests while allowing the app developer to only request the minimal amount of permissions needed at any point in time. – Josef Vancura Jan 13 '21 at 08:38
  • @JosefVancura: The current presentation to the user is by group. Presumably, Google wanted the flexibility to use a per-permission presentation in the future, should that prove necessary. – CommonsWare Jan 13 '21 at 11:43