Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. // Here, thisActivity is the current activity
  2. if (ContextCompat.checkSelfPermission(thisActivity,
  3. Manifest.permission.READ_CONTACTS)
  4. != PackageManager.PERMISSION_GRANTED) {
  5.  
  6. // Should we show an explanation?
  7. if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
  8. Manifest.permission.READ_CONTACTS)) {
  9.  
  10. // Show an expanation to the user *asynchronously* -- don't block
  11. // this thread waiting for the user's response! After the user
  12. // sees the explanation, try again to request the permission.
  13.  
  14. } else {
  15.  
  16. // No explanation needed, we can request the permission.
  17.  
  18. ActivityCompat.requestPermissions(thisActivity,
  19. new String[]{Manifest.permission.READ_CONTACTS},
  20. MY_PERMISSIONS_REQUEST_READ_CONTACTS);
  21.  
  22. // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
  23. // app-defined int constant. The callback method gets the
  24. // result of the request.
  25. }
  26. }
  27. Handle the permissions request response
  28.  
  29. @Override
  30. public void onRequestPermissionsResult(int requestCode,
  31. String permissions[], int[] grantResults) {
  32. switch (requestCode) {
  33. case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
  34. // If request is cancelled, the result arrays are empty.
  35. if (grantResults.length > 0
  36. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  37.  
  38. // permission was granted, yay! Do the
  39. // contacts-related task you need to do.
  40.  
  41. } else {
  42.  
  43. // permission denied, boo! Disable the
  44. // functionality that depends on this permission.
  45. }
  46. return;
  47. }
  48.  
  49. // other 'case' lines to check for other
  50. // permissions this app might request
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement