Android marshmallow request permission?

后端 未结 24 2396
無奈伤痛
無奈伤痛 2020-11-21 06:40

I am currently working on an application that requires several \"dangerous\" permissions. So I tried adding \"ask for permission\" as required in Android Marshmallow(API Lev

24条回答
  •  半阙折子戏
    2020-11-21 07:16

    RUNTIME PERMISSION IN ANDROID

    public void onClick(View view) {
        if (view.getId() == shareButton.getId()) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                this.shareLog();
            } else {
                ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, LogConstants.PERMISSION_REQ_WRITE_EXTERNAL_STORAGE);
            }
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == LogConstants.PERMISSION_REQ_WRITE_EXTERNAL_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                this.shareLog();
            }
        }
    }
    

    OnClick method is to check runtime permission

    and if Permission is restricted then it request permission

提交回复
热议问题