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.
- Add below <uses-permission> XML tag in AndroidManifest.xml file.
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
- 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);
- 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.
- 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.
- You can see this example demo video at the end of this article.
- In the above example when you click the button, if this application does not has android.permission.WRITE_SETTINGS permission, then it will pop up the Can modify system settings panel to let the user change the permission to yes.
- 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.
- 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(); } } }); } }
- You can see this example demo video on URL https://youtube.com/shorts/tMc7Mqc_fak?feature=share.
Thanks, this was very helpful!
Hi, my question is what examples can i do write settings?
Allowing your app to turn bluetooth on and off.