1

I want write settings permission in order to start portable WLAN hotspot...

this is what i got,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(getApplicationContext())) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, 200);
            }
        }

this works fine until i press back button after i grant permission.

after pressing back button resultCode is 0 always, therefore further code is not working properly.

  • possible work around is to check on result if the wifi state is changed? https://stackoverflow.com/questions/9065592/how-to-detect-wifi-tethering-state – Mercato Jun 06 '17 at 12:13

2 Answers2

3

An ACTION_MANAGE_WRITE_SETTINGS is not documented to return a result. Most activities do not return a result.

Maybe it's not documented, but as you noticed yourself there is always a result for ACTION_MANAGE_WRITE_SETTINGS. While the resultCode is always 0, the result does hand you back your original requestCode.

So you can use this to check in onActivityResult whether you now have the permission that you asked for.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case REQUEST_CODE_WRITE_SYSTEM:
                boolean canWrite = Settings.System.canWrite(this);
                // Handle the result here
                break;
        }
  }
user8118328
  • 653
  • 1
  • 7
  • 10
0

after pressing back button resultCode is 0 always

An ACTION_MANAGE_WRITE_SETTINGS is not documented to return a result. Most activities do not return a result.

Do not call startActivityForResult() — just use startActivity(). When needed, call Settings.System.canWrite(this) to see if you have permission.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I don't think this should be the accepted answer. Usually, we use Settings. ACTION_MANAGE_WRITE_SETTINGS just after checking the result of canWrite(), after attempting to do a prohibited operation. Therefore, in terms of UX the best way to do it would be to continue the operation - now allowed - just after the user comes back from the settings activity. That's why it's a good idea to use startActivityFromResult() and look at the request code (ignoring the result code) – Bernardo do Amaral Teodosio Jan 17 '22 at 12:36