Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. protected void askAllPermissions() {
  2. Toast.makeText(MainActivity.this, "before 1", Toast.LENGTH_LONG).show();
  3. while(!askPermission(Manifest.permission.ACCESS_COARSE_LOCATION)) {
  4. try {
  5. Thread.sleep(1000);
  6. } catch (InterruptedException e)
  7. {
  8. e.printStackTrace();
  9. }
  10. }
  11. Toast.makeText(MainActivity.this, "after 1", Toast.LENGTH_LONG).show();
  12.  
  13. if (permissionValue != 1) {
  14. Toast.makeText(MainActivity.this, R.string.permission_required, Toast.LENGTH_LONG).show();
  15. finish();
  16. }
  17.  
  18. dialogDisplayed = false;
  19. permissionValue = 0;
  20. permissionAnswered=false;
  21.  
  22. Toast.makeText(MainActivity.this, "before 2", Toast.LENGTH_LONG).show();
  23.  
  24. while(!askPermission(Manifest.permission.READ_PHONE_STATE)) {
  25. try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
  26. }
  27.  
  28. Toast.makeText(MainActivity.this, "after 2", Toast.LENGTH_LONG).show();
  29.  
  30. if (permissionValue != 1) {
  31. Toast.makeText(MainActivity.this, R.string.permission_required, Toast.LENGTH_LONG).show();
  32. finish();
  33. }
  34.  
  35. dialogDisplayed = false;
  36. permissionValue = 0;
  37. permissionAnswered=false;
  38.  
  39. while(!askPermission(Manifest.permission.CALL_PHONE)) {
  40. try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
  41. }
  42.  
  43. if (permissionValue != 1) {
  44. Toast.makeText(MainActivity.this, R.string.permission_required, Toast.LENGTH_LONG).show();
  45. finish();
  46. }
  47.  
  48.  
  49. }
  50.  
  51. protected boolean askPermission(String permmission) {
  52. if (ContextCompat.checkSelfPermission(MainActivity.this, permmission) != PackageManager.PERMISSION_GRANTED) {
  53. if (!dialogDisplayed) {
  54. ActivityCompat.requestPermissions(MainActivity.this,
  55. new String[]{permmission},
  56. REQUEST_ACCESS_RESPONSE);
  57. dialogDisplayed = true;
  58. }
  59. // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
  60. // app-defined int constant. The callback method gets the
  61. // result of the request.
  62. } else {
  63. permissionValue = 1;
  64. permissionAnswered=true;
  65. }
  66.  
  67. return permissionAnswered;
  68. }
  69.  
  70. @Override
  71. public void onRequestPermissionsResult(int requestCode,
  72. String permissions[], int[] grantResults) {
  73. switch (requestCode) {
  74. case REQUEST_ACCESS_RESPONSE: {
  75. // If request is cancelled, the result arrays are empty.
  76. if (grantResults.length > 0
  77. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  78. permissionAnswered=true;
  79. // permission was granted, yay! Do the
  80. // contacts-related task you need to do.
  81.  
  82. } else {
  83.  
  84. // permission denied, boo! Disable the
  85. // functionality that depends on this permission.
  86. }
  87. return;
  88. }
  89.  
  90. // other 'case' lines to check for other
  91. // permissions this app might request
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement