How To Grant Write Settings Permission In Android

When you write android app, you sometimes may need to change system settings. For example change the android device screen brightness. But such action need your app has android.permission.WRITE_SETTINGS permission, otherwise you may encounter below exception.

android security exception need write settings permission

The exception message is : 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 current android device.
    boolean settingsCanWrite = Settings.System.canWrite(context);
  3. If above code return false which means your app do not has the write settings permission, then you need to start activity Settings.ACTION_MANAGE_WRITE_SETTINGS to let user change the permission manually.
  4. After the permission has been changed, you can type back menu go to your android application to continue.

2. Change WRITE_SETTINGS Permission For Android App Example.

change write settings permission for android application new

In above example when you click the button, if this application do not has android.permission.WRITE_SETTINGS permission, it will then popup the Can modify system settings panel to let user to change the permission to yes.

If the application has write settings permission, it will popup an alert dialog and show a message.

3. Change Write Settings Permission Example Source Code.

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>

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>

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();
                }
            }
        });
    }
}
(Visited 8,707 times, 2 visits today)
READ :   Android SurfaceView Drawing Example

Leave a Reply

Your email address will not be published. Required fields are marked *

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