简体   繁体   中英

Closing the "All files access" dialog when user allows access to all files in android

Presently, in my application, when the user is asked to provide "Access all files" permission, the dialog opens and remains on screen even when the user has allowed permission. Below provided screenshot shows the dialog I am referring to:-

在此处输入图像描述

I would like to add something in my code, which closes this dialog and opens target activity screen as soon as the user enables the toggle of "Allow access to all files".

GrantPermissionsActivity:-

public class GrantPermissionsActivity extends AppCompatActivity {

     private static final int PERMISSION_REQUEST_CODE = 1;
     MaterialButton cancelMaterialButton, grantMaterialButton;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_grant_permissions);

         findViews();

         initViews();
      }

      private void findViews() {
      cancelMaterialButton = findViewById(R.id.materialButtonCancel);
      grantMaterialButton = findViewById(R.id.materialButtonGrant);
   }

   private void initViews() {

      if (checkPermission()){
         Intent intent = new Intent(GrantPermissionsActivity.this,PasswordActivity.class);
      }

      cancelMaterialButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Toast.makeText(GrantPermissionsActivity.this, "Need to give permission!", Toast.LENGTH_SHORT).show();
              finish();
              overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
          }
      });

      grantMaterialButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (checkPermission()) {
                  Toast.makeText(GrantPermissionsActivity.this, "Permission already granted", Toast.LENGTH_SHORT).show();

              } else if (!checkPermission()) {
                requestPermission();
              }
          }
      });
  }

  private boolean checkPermission() {
      if (SDK_INT >= Build.VERSION_CODES.R) {
          return Environment.isExternalStorageManager();
      } else {
          int result = ContextCompat.checkSelfPermission(GrantPermissionsActivity.this, READ_EXTERNAL_STORAGE);
          int result1 = ContextCompat.checkSelfPermission(GrantPermissionsActivity.this, WRITE_EXTERNAL_STORAGE);
          return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
      }
  }

  private void requestPermission() {
      if (SDK_INT >= Build.VERSION_CODES.R) {
          try {
              Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
              intent.addCategory("android.intent.category.DEFAULT");
              intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
              startActivityForResult(intent, 2296);
          } catch (Exception e) {
              Intent intent = new Intent();
              intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
              startActivityForResult(intent, 2296);
          }
      } else {
          //below android 11
          ActivityCompat.requestPermissions(GrantPermissionsActivity.this, new String[]{WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
      }
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
      if (requestCode == PERMISSION_REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
             // perform action when allow permission success

              Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
          } else {
              Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
          }
      }
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);

  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)   {
      Log.d(TAG, "onActivityResult: Reachedinresult");
      if (requestCode == 2296 && resultCode == RESULT_OK) {
          Log.d(TAG, "onActivityResult: Reached request code");
          if (SDK_INT >= Build.VERSION_CODES.R) {
              Log.d(TAG, "onActivityResult: Reached SDKINT");
              if (Environment.isExternalStorageManager()) {
                  // perform action when allow permission success
                  /*Intent intent = new Intent(GrantPermissionsActivity.this,PasswordActivity.class);
                  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  startActivity(intent);*/
                  Log.d(TAG, "onActivityResult: Reached");

              } else {
                  checkPermission();
                  Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
              }
          }
      }
      super.onActivityResult(requestCode, resultCode, data);
  }
}

Please let me know the changes in the above code.

Also, check the below sample for reference:-

在此处输入图像描述

  • Replace the method requestPermission() to

     private void requestPermission() { if (SDK_INT >= Build.VERSION_CODES.R) { try { Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); intent.addCategory("android.intent.category.DEFAULT"); intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName()))); startActivityForResult(intent, 2296); } catch (Exception e) { Intent intent = new Intent(); intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); startActivityForResult(intent, 2296); new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { if(Environment.isExternalStorageManager()) { runOnUiThread(new Runnable() { @Override public void run() { // perform action when allow permission success in android 11 } }); this.cancel(); } } },2000,1000); } } else { //below android 11 ActivityCompat.requestPermissions(GrantPermissionsActivity.this, new String[]{WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); }}
  • If you want to start an activity from there, add flag Intent.FLAG_ACTIVITY_NEW_TASK in the Intent object, otherwise it may not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM