Last active
May 21, 2017 15:48
-
-
Save valposv/82144b09d1464315a22f09fddc55f9a7 to your computer and use it in GitHub Desktop.
Permission request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MapActivity extends AppCompatActivity | |
| implements OnMapReadyCallback{ | |
| public final int PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 0; | |
| private GoogleMap _googleMap; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_map); | |
| SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() | |
| .findFragmentById(R.id.map); | |
| mapFragment.getMapAsync(this); | |
| } | |
| @Override | |
| public void onMapReady(GoogleMap googleMap) { | |
| _googleMap = googleMap; | |
| if (ContextCompat.checkSelfPermission(MapActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) | |
| != PackageManager.PERMISSION_GRANTED) { | |
| ActivityCompat.requestPermissions(MapActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, | |
| PERMISSION_REQUEST_ACCESS_FINE_LOCATION); | |
| } else { | |
| _googleMap.setMyLocationEnabled(true); | |
| } | |
| } | |
| @Override | |
| public void onRequestPermissionsResult(int requestCode, | |
| String permissions[], int[] grantResults) { | |
| switch (requestCode) { | |
| case PERMISSION_REQUEST_ACCESS_FINE_LOCATION: { | |
| if (grantResults.length > 0 | |
| && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
| _googleMap.setMyLocationEnabled(true); | |
| } else { | |
| Snackbar.make(findViewById(android.R.id.content), "Access to location was not granted", Snackbar.LENGTH_LONG) | |
| .setAction("Action", null).show(); | |
| } | |
| return; | |
| } | |
| // other 'case' lines to check for other | |
| // permissions this app might request | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment