How to approve WRITE_SETTINGS authority above marshmallow version

Asked 2 years ago, Updated 2 years ago, 32 views

When I was making the alarm app, I made the part where I chose the ringtone The permission part didn't approve it So I looked it up, and I heard that marshmallow needs to work separately to approve the permission, so I was looking for related data https://stackoverflow.com/questions/32083410/cant-get-write-settings-permission There was someone who posted the same question, so I checked it out It works fine with 7.0 versions, while marshmallow still fails. There's a person who has the same problem on that site.

The code is as below, but which part is the problem?

I heard from others that the marshmallow version did not approve even if it was approved.

 @RequiresApi(api=Build).VERSION_CODES.M)
    public void onClick(View v) {

        if (v.equals(b)) {
            boolean permission;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // For marshmallow or higher
                permission = Settings.System.canWrite(getApplicationContext()); // check for authorization
            } } else {
                permission = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
            }

            If (permission) { //Someone's version comes right here, but Marshmallow comes here after approval, but it's an error anyway

                Intent in = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                startActivityForResult(in, 0);

            } else { //if not


                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { // Process for approving

                    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    intent.setData(Uri.parse("package:" + getApplicationContext().getPackageName()));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

                } } else {

                    requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS}, 1000);
                }
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 1000 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplication(), "Marshmallow or Nougat version or lower", Toast.LENGTH_SHORT).show();
        } } else {
                Toast.makeText(getApplication(), "Request denied," Toast.LENGTH_SHORT).show();
            }


    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 0:
                Toast.makeText(getApplication(), "Successfully set", Toast.LENGTH_SHORT).show();
                Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, uri);
                break;
        }
    }

android marshmallow

2022-09-22 19:45

1 Answers

I think I can answer better if you upload the error log as well.

First of all, I think there's too much movement coming to request permissions().

I understand that versions without runtime permission requests accept all permission requests during installation, so you do not need to check the version before requestPermissions().

So, simply check whether you have permission or not, and if you don't, you can operate the code if you have permission request.

 // Return above the fragment.
    REQUEST_PERMISSION_AUDIO_RECORD = Constant;
    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_PERMISSION_AUDIO_RECORD);
    } } else {
        doSomthing();
    }


2022-09-22 19:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.