java.lang.SecurityException: Permission denial: writing to settings requires android.permission.WRITE_SETTINGS

13,841

WRITE_SECURE_SETTINGS is not for use by third-party applications. No app outside of the system/firmware can get that permission regardless whether you are on rooted or production builds.

Please check this answer

Share:
13,841
Manohar Udgiri
Author by

Manohar Udgiri

Updated on June 05, 2022

Comments

  • Manohar Udgiri
    Manohar Udgiri about 2 years

    This is my code by which i am building airplan mode On/OFF on Platform 5.0.0 below is mine Activity.java source code please suggest the changes which i need to do in manifeast.xml for access the permission.

    package com.rjil.airplanemodeonoff;
    
    import android.content.Intent;
    import android.os.Build;
    import android.provider.Settings;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TextView;
    import android.widget.ToggleButton;
    
    public class MainActivity extends AppCompatActivity {
        // constants
        static final String STATUS_ON = "Airplane Mode: ON";
        static final String STATUS_OFF = "Airplane Mode: OFF";
    
        static final String TURN_ON = "Turn ON";
        static final String TURN_OFF = "Turn OFF";
    
        // controls
        TextView TVAirplaneMode;
        ToggleButton tBAirplane;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // load controls
            TVAirplaneMode = (TextView)findViewById(R.id.TVAirplaneMode);
            tBAirplane = (ToggleButton)findViewById(R.id.tBAirplane);
            // update UI at first time loading
            updateUI(isAirplaneMode());
            // set click event for button
            tBAirplane.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // check current state first
                    boolean state = isAirplaneMode();
                    // toggle the state
                    if (state) toggleAirplaneMode(0, state);
                    else toggleAirplaneMode(1, state);
                    // update UI to new state
                    updateUI(!state);
                }
            });
        }
        public void toggleAirplaneMode(int value, boolean state) {
            // toggle airplane mode
            //check the version
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //if less than verson 4.2
                Settings.System.putInt(
                        getContentResolver(),
                        Settings.System.AIRPLANE_MODE_ON, value);
            } else {
                Settings.Global.putInt(
                        getContentResolver(),
                        Settings.Global.AIRPLANE_MODE_ON, value);
            }
            // broadcast an intent to inform
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", !state);
            sendBroadcast(intent);
        }
    
        public void updateUI(boolean state) {
            //set text according to state
            if(state) {
                TVAirplaneMode.setText(STATUS_ON);
                tBAirplane.setText(TURN_OFF);
            } else {
                TVAirplaneMode.setText(STATUS_OFF);
                tBAirplane.setText(TURN_ON);
            }
        }
    
    
        public boolean isAirplaneMode() {
            //check the version
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {//if less than verson 4.2
                return Settings.System.getInt(getContentResolver(),
                        Settings.System.AIRPLANE_MODE_ON, 0) != 0;
            } else {
                return Settings.Global.getInt(getContentResolver(),
                        Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
    
            }
        }
    
    
    
    }
    
    • Ravi Jaggarapu
      Ravi Jaggarapu over 8 years
      can u please share your manifest code .
    • IntelliJ Amiya
      IntelliJ Amiya over 8 years
      <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
    • Ravi Jaggarapu
      Ravi Jaggarapu over 8 years
    • Ravi Jaggarapu
      Ravi Jaggarapu over 8 years
      If you found any security exception in further it may help to find your missing things, and you must need to know about why we need to keep permission declaration in manifest. Their is another case for occurring this kind of exceptions check here once: stackoverflow.com/q/13045283/2983864