Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void askAllPermissions() {
- Toast.makeText(MainActivity.this, "before 1", Toast.LENGTH_LONG).show();
- while(!askPermission(Manifest.permission.ACCESS_COARSE_LOCATION)) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- Toast.makeText(MainActivity.this, "after 1", Toast.LENGTH_LONG).show();
- if (permissionValue != 1) {
- Toast.makeText(MainActivity.this, R.string.permission_required, Toast.LENGTH_LONG).show();
- finish();
- }
- dialogDisplayed = false;
- permissionValue = 0;
- permissionAnswered=false;
- Toast.makeText(MainActivity.this, "before 2", Toast.LENGTH_LONG).show();
- while(!askPermission(Manifest.permission.READ_PHONE_STATE)) {
- try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
- }
- Toast.makeText(MainActivity.this, "after 2", Toast.LENGTH_LONG).show();
- if (permissionValue != 1) {
- Toast.makeText(MainActivity.this, R.string.permission_required, Toast.LENGTH_LONG).show();
- finish();
- }
- dialogDisplayed = false;
- permissionValue = 0;
- permissionAnswered=false;
- while(!askPermission(Manifest.permission.CALL_PHONE)) {
- try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
- }
- if (permissionValue != 1) {
- Toast.makeText(MainActivity.this, R.string.permission_required, Toast.LENGTH_LONG).show();
- finish();
- }
- }
- protected boolean askPermission(String permmission) {
- if (ContextCompat.checkSelfPermission(MainActivity.this, permmission) != PackageManager.PERMISSION_GRANTED) {
- if (!dialogDisplayed) {
- ActivityCompat.requestPermissions(MainActivity.this,
- new String[]{permmission},
- REQUEST_ACCESS_RESPONSE);
- dialogDisplayed = true;
- }
- // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
- // app-defined int constant. The callback method gets the
- // result of the request.
- } else {
- permissionValue = 1;
- permissionAnswered=true;
- }
- return permissionAnswered;
- }
- @Override
- public void onRequestPermissionsResult(int requestCode,
- String permissions[], int[] grantResults) {
- switch (requestCode) {
- case REQUEST_ACCESS_RESPONSE: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- permissionAnswered=true;
- // permission was granted, yay! Do the
- // contacts-related task you need to do.
- } else {
- // permission denied, boo! Disable the
- // functionality that depends on this permission.
- }
- return;
- }
- // other 'case' lines to check for other
- // permissions this app might request
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement