How To Grant Write Settings Permission In Android

When you write an android app, you sometimes may need to change system settings. For example, change the android device screen brightness. But such action needs your app to have android.permission.WRITE_SETTINGS permission, otherwise you may encounter the exception with the message java.lang.SecurityException: com.dev2qa.example was not granted this permission: android.permission.WRITE_SETTINGS.

1. Add Android WRITE_SETTINGS Permission Steps.

  1. Add below <uses-permission> XML tag in AndroidManifest.xml file.
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  2. Call Settings.System.canWrite(context) to check whether your android app can write settings in the current Android device.
    boolean settingsCanWrite = Settings.System.canWrite(context);
  3. If the above code return false which means your app does not has the write settings permission, then you need to start activity Settings.ACTION_MANAGE_WRITE_SETTINGS to let users change the permission manually.
  4. After the permission has been changed, you can type the back menu to go to your android application to continue.

2. Change WRITE_SETTINGS Permission For Android App Example.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/tMc7Mqc_fak

  1. In the above example when you click the button, if this application does not has android.permission.WRITE_SETTINGS permission, it will then popup the Can modify system settings panel to let the user change the permission to yes.
  2. If the application has the write settings permission, it will pop up an alert dialog and show a message.

3. Change Write Settings Permission Example Source Code.

  1. AndroidManifest.xml
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.dev2qa.example">
    
        <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity android:name=".permission.WriteSettingsPermissionActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
  2. Main layout XML file: activity_write_settings_permission.xml
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/write_settings_permission_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Change Write Settings Permission"/>
    
    </LinearLayout>
  3. Main activity java file: WriteSettingsPermissionActivity.java
    package com.dev2qa.example.permission;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    
    import com.dev2qa.example.R;
    
    public class WriteSettingsPermissionActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_write_settings_permission);
    
            setTitle("dev2qa.com - Add Write Settings Permission Example");
    
            // Get the button and add onClickListener.
            final Button writeSettingsPermissionButton = (Button)findViewById(R.id.write_settings_permission_button);
            writeSettingsPermissionButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    Context context = getApplicationContext();
    
                    // Check whether has the write settings permission or not.
                    boolean settingsCanWrite = Settings.System.canWrite(context);
    
                    if(!settingsCanWrite) {
                        // If do not have write settings permission then open the Can modify system settings panel.
                        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        startActivity(intent);
                    }else {
                        // If has permission then show an alert dialog with message.
                        AlertDialog alertDialog = new AlertDialog.Builder(WriteSettingsPermissionActivity.this).create();
                        alertDialog.setMessage("You have system write settings permission now.");
                        alertDialog.show();
                    }
                }
            });
        }
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Ole
Ole

Thanks, this was very helpful!

Romell Domínguez
Romell Domínguez

Hi, my question is what examples can i do write settings?

Sphelele
Sphelele

Allowing your app to turn bluetooth on and off.

3
0
Would love your thoughts, please comment.x
()
x