为什么方法“savefile”在接受权限时不运行?

pkwftd7m  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(340)

我的android应用程序中有以下活动:

public class DisplaySettingsActivity extends AppCompatActivity implements View.OnClickListener {
    private H300sVoipSettings settings;

    Button saveIntoFile;
    TextView msg;

    private static final int REQUEST_STORAGE_ID=1000;

    private ActivityResultLauncher<String> requestPermissionLauncher;

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

        msg = (TextView) findViewById(R.id.saveMsg);

        requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
            Log.d("H300s","Permissions Callback");

            if (isGranted) {
                Log.d("H300s","Permission Accepted 2");
                saveFile();
            } else {
                permissionSaveDenied();
            }
        });

        this.settings = (H300sVoipSettings) getIntent().getSerializableExtra("H300sVoipSettings");
    }

 private void saveMsgHandler(String savePath){
        if (savePath == null) {
            msg.setText(R.string.could_not_save_settings);
            int errorColor = ContextCompat.getColor(this, R.color.error);
            msg.setBackgroundColor(errorColor);
        } else {
            msg.setText(R.string.save_success);
            int success = ContextCompat.getColor(this, R.color.success);
            msg.setBackgroundColor(success);
        }
        msg.setVisibility(View.VISIBLE);
        this.saveIntoFile.setEnabled(true);
    }

    private void permissionSaveDenied(){
        msg.setVisibility(View.VISIBLE);
        msg.setText(R.string.could_not_save_settings);
        int errorColor = ContextCompat.getColor(this, R.color.error);
        msg.setBackgroundColor(errorColor);
        this.saveIntoFile.setEnabled(true);
    }

    @Override
    public void onClick(View v) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Log.d("H300s","Permission Accepted");
            saveFile();
        } else {
            ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_STORAGE_ID);
        }
    }
 private void saveFile(){
        Log.d("Η300s","Saving");
        String state = Environment.getExternalStorageState();
        if (!Environment.MEDIA_MOUNTED.equals(state)) {
            Log.e("H300s","Unable to detect external storage");
            saveMsgHandler(null);
            return;
        }

        this.saveIntoFile.setEnabled(false);

        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyMMdd");
        File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        file = new File( file.getAbsolutePath(),"voip_h300s_"+pattern.format(LocalDate.now())+".txt");
        Log.d("H300s",file.toString());
        try {
            file.createNewFile();
            Log.d("H300s","Saving");
            this.settings.save(file);
            Log.d("H300s","Saved");
            Log.d("H300s",file.getAbsolutePath());
            saveMsgHandler(file.getAbsolutePath());
        } catch (Exception e) {
            Log.e("H300s",e.toString());
            Log.e("H300s",Log.getStackTraceString(e));
            saveMsgHandler(null);
        }
    }
}

但一旦用户接受了权限,以下代码似乎就不会被调用:

requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
            Log.d("H300s","Permissions Callback");

            if (isGranted) {
                Log.d("H300s","Permission Accepted 2");
                saveFile();
            } else {
                permissionSaveDenied();
            }
        });

尤其是一旦用户授予 WRITE_EXTERNAL_STORAGE 权限然后是函数 saveFile 好像没人叫。有人知道为什么会这样吗?

xtupzzrd

xtupzzrd1#

要使其正常工作,您需要执行以下操作:

第1步

首先要确保你的 build.gradle 以下行:

implementation 'androidx.activity:activity-ktx:1.2.0'
  implementation 'androidx.fragment:fragment:1.3.0'

第2步

然后在 onClick 函数而不是: ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_STORAGE_ID); 使用:

requestPermissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE );

相关问题