1


I've found a lot of examples here but no one works for me...I can't understand what's wrong.
what I need is to enter the "airplane mode" in my app and I search for a full example.
What I have done is
1. request permission in manifest (I've seen I need "WRITE_SETTINGS" permission)

<uses-permission-sdk-m android:name="android.permission.WRITE_SETTINGS" />

2. in the code, when I want to change Airplane Mode, I check for permission

private void setAirplaneMode() {
    boolean permission;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        permission = Settings.System.canWrite(this);
    } else {
        permission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
    }

    if (permission) {
        //here I go to activate the airplane mode
        setAirplaneModeIntent(true);
    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_SETTINGS)) {
            //I need to explain why I want permission
            //TODO
        } else {
            ActivityCompat.requestPermissions(this, new String[] android.Manifest.permission.WRITE_SETTINGS}, MY_PERMISSION_REQUEST_WRITE_SETTINGS);
        }
    }


3. I override the "onRequestPermissionsResult"

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResult) {

    switch (requestCode) {
        case MY_PERMISSION_REQUEST_WRITE_SETTINGS: {
            if( grantResult.length > 0 &&
                grantResult[0] == PackageManager.PERMISSION_GRANTED) {
                setAirplaneModeIntent(true);
            }
        }
    }
}


4. in the "setAirplaneModeIntent" I set the Airplane mode

private void setAirplaneModeIntent(boolean activateAirplaneMode) {
    Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
            activateAirplaneMode ? 1 : 0);        

    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", activateAirplaneMode);
    sendBroadcast(intent);
}



when I debug, I enter in the "ActivityCompat.requestPermissions" code but when I reach the "onRequestPermissionsResult" the result is not PackageManager.PERMISSION_GRANTED.
What's wrong?

EDIT
I solve the "requestPermissions" part. For the "WRITE_SETTINGS" permission you need the ACTION_MANAGE_WRITE_SETTINGS intent.
See here:

  1. https://developer.android.com/reference/android/provider/Settings.html#ACTION_MANAGE_WRITE_SETTINGS
  2. https://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS
  3. http://stackoverflow.com/questions/39224303/write-settings-permission-not-granted-marshmallow-android


Anyway I got an error in the point 4, when I send the broadcast.
If I don't sent the broadcast, the "Settings.System.putInt" command seems doing nothing.
Someone can help me?

EDIT 2
I surrender. The only solution I have found that works is to open the network settings in this way

Intent intentAirplaneMode = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
intentAirplaneMode.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentAirplaneMode);
DanB
  • 57
  • 7
  • In short, is Google Signin blocked by airplane mode? The only way is to ask the user to enable this mode? – quangkid Mar 06 '18 at 10:17

0 Answers0