Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีปรับความสว่างหน้าจอแบบโปรแกรมใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีควบคุมความสว่างหน้าจอ (Screen Brightness) โดยโปรแกรมผ่าน SeekBar ใช้ Settings.System.SCREEN_BRIGHTNESS

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: จัดทำ Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:padding="8dp"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ปรับความสว่างหน้าจอ" />
    <SeekBar
        android:id="@+id/seekBar"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.java

import android.content.Context;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {
    SeekBar lightBar;
    Context context;
    int brightness;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        lightBar = findViewById(R.id.seekBar);
        context = getApplicationContext();
        
        // อ่านค่าความสว่างปัจจุบัน (0-255)
        brightness = Settings.System.getInt(context.getContentResolver(),
            Settings.System.SCREEN_BRIGHTNESS, 0);
        lightBar.setProgress(brightness);
        
        lightBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Settings.System.putInt(context.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS, progress);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });
    }
}

ขั้นตอนที่ 4: AndroidManifest.xml - สิทธิ์สำคัญ

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

ข้อสังเกตสำคัญ

  • ค่าความสว่างอยู่ในช่วง 0-255
  • ต้องขอสิทธิ์ WRITE_SETTINGS (Protected Permission)
  • Android 6.0+ ต้องขออนุญาตผู้ใช้ผ่าน Settings.ACTION_MANAGE_WRITE_SETTINGS
  • การเปลี่ยนแปลงจะมีผลต่อระบบทั้งหมด (System-wide)

ขอสิทธิ์ WRITE_SETTINGS แบบ Runtime (API 23+)

if (!Settings.System.canWrite(this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
    intent.setData(Uri.parse("package:" + getPackageName()));
    startActivity(intent);
}

ทางเลือก: ปรับความสว่างเฉพาะในแอป (WindowManager)

// เปลี่ยนแค่ Activity นี้ ไม่กระทบระบบ
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = progress / 255.0f; // 0.0 - 1.0
getWindow().setAttributes(layoutParams);

การทดสอบ

รันแอป เลื่อน SeekBar หน้าจอจะสว่าง/มืดทันที