简体   繁体   English

当用户允许访问 android 中的所有文件时关闭“所有文件访问”对话框

[英]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:- 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将方法requestPermission()替换为

     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.如果你想从那里开始一个活动,在Intent object 中添加标志Intent.FLAG_ACTIVITY_NEW_TASK ,否则它可能无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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