Kaveh.R
Kaveh.R

Reputation: 1

java.lang.SecurityException: was not granted this permission: android.permission.WRITE_SETTINGS

I going to change some system setting in android and i use this code :

ContentResolver cr = getContentResolver();
Settings.System.putInt(cr, Settings.System.HAPTIC_FEEDBACK_ENABLED, 0);

this code used for change screen Brightness use Brightness sensor,
but in android 6 I get this exception

java.lang.SecurityException: com.vpn.sabalan was not granted  this permission: android.permission.WRITE_SETTINGS.

i can use this method to get permission from user , but i need get permission programmetically can any one help me ?

 private void showBrightnessPermissionDialog(  )
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !android.provider.Settings.System.canWrite(this))
        {
            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:"+getPackageName()));
            startActivity(intent);
        }
    }

Upvotes: 0

Views: 5212

Answers (2)

Hitesh Sahu
Hitesh Sahu

Reputation: 45062

Update Android Marshmallow and Higher

You can start System settings to grant Write System Settings. Once this permission is grant by user you can set brightness without any issues

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.System.canWrite(this)) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivity(intent);
                }
            }

Upvotes: 1

Sam
Sam

Reputation: 5392

Follow the step by step provided in documentation. It is very thorough.

https://developer.android.com/training/permissions/requesting.html

All you have to do is request permission, and override the callback for onRequestPermissionsResult to check if you got it or not. If you did, then you are good to go. You still need it in your manifest though or it won't work.

UPDATE to show details based on your comments.

public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{

  private static final int REQUEST_WRITE_PERMISSION = 1001;

  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {            
        doFileWork();
    }else{
        //handle user denied permission, maybe dialog box to user
    }
  }

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

  private void requestPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
    } else {
        doFileWork();
    }
  }
}

There are also many good libraries out there that wrap this callback context if you really want to go that route, but it isn't that complex. Make sure you also have write permission in your Manifest.

Upvotes: 0

Related Questions