info@androidpaper.co.in

How to Control the screen brightness using volume keys

To increase or decrease screen brightness using volume keys programmatically in an Android app, you'll need to capture the volume key events and adjust the system screen brightness accordingly. Here's a step-by-step guide on how you can achieve this:

Create a new Android project or open an existing one in your preferred IDE (e.g., Android Studio). In your app's AndroidManifest.xml file, make sure you have the following permission to modify system settings:

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

Open the activity or fragment where you want to implement the volume key handling (e.g., MainActivity.java). Inside the activity or fragment, override the dispatchKeyEvent() method to capture the volume key events:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    int action = event.getAction();

    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        if (action == KeyEvent.ACTION_DOWN) {
            // Handle volume key press
            adjustScreenBrightness(keyCode);
        }
        return true; // Consume the key event
    }

    return super.dispatchKeyEvent(event);
}

Implement the adjustScreenBrightness() method to increase or decrease the screen brightness based on the volume key press:

private void adjustScreenBrightness(int keyCode) {
    ContentResolver contentResolver = getContentResolver();
    int currentBrightness = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, 0);

    int deltaBrightness = 10; // You can adjust this value according to your preference

    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        currentBrightness += deltaBrightness;
    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        currentBrightness -= deltaBrightness;
    }

    // Make sure the brightness value is within the valid range (0-255)
    if (currentBrightness < 0) {
        currentBrightness = 0;
    } else if (currentBrightness > 255) {
        currentBrightness = 255;
    }

    // Update the system screen brightness
    Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, currentBrightness);

    // Apply the new brightness level
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.screenBrightness = currentBrightness / 255f;
    getWindow().setAttributes(layoutParams);
}

Starting from Android 6.0 (API level 23) and above, the WRITE_SETTINGS permission is classified as a dangerous permission, which means it cannot be granted at runtime. However, you can direct the user to the system settings page where they can manually grant permission for your app. Here's how you can open the system settings page for your app using an intent:

private static final int REQUEST_CODE_WRITE_SETTINGS = 1;

// Method to check if the WRITE_SETTINGS permission is granted
private boolean hasWriteSettingsPermission() {
    return Settings.System.canWrite(getApplicationContext());
}

// Method to request the WRITE_SETTINGS permission
private void requestWriteSettingsPermission() {
    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
    intent.setData(Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, REQUEST_CODE_WRITE_SETTINGS);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_WRITE_SETTINGS) {
        if (hasWriteSettingsPermission()) {
            // The WRITE_SETTINGS permission has been granted
            // You can now proceed with changing the screen brightness
         adjustScreenBrightness(keyCode);
        } else {
            // The WRITE_SETTINGS permission was not granted
            // Handle this situation accordingly
           Toast.makeText(getApplicationContext(), "You did not give permission", Toast.LENGTH_SHORT).show();
        }
    }
}

In your code, when you call the requestWriteSettingsPermission() method, it will open the system settings page for your app, where the user can grant the WRITE_SETTINGS permission. Once the user grants permission, the onActivityResult() method will be triggered. You can check if the permission was granted using the hasWriteSettingsPermission() method and proceed with changing the screen brightness if the permission was granted.