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