今日、この問題を解決しました。
最初に AndroidManifest.xml ファイルにパーミッションを設定する必要があります:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
ファイルに配置する正確な場所はどこですか?
<manifest>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application>
<activity />
</application>
</manifest>
この権限は、他のアプリケーションにも影響する設定を変更できることを示しています。
明るさの自動モードのオンとオフを設定できるようになりました
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); //this will set the automatic mode on
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); //this will set the manual mode (set the automatic mode off)
現在、自動モードがオンまたはオフになっていますか? 情報を得ることができます
int mode = -1;
try {
mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); //this will return integer (0 or 1)
} catch (Exception e) {}
そのため、明るさを手動で変更したい場合は、最初に手動モードに設定してから、明るさを変更する必要があります。
注: SCREEN_BRIGHTNESS_MODE_AUTOMATIC は 1 です
注: SCREEN_BRIGHTNESS_MODE_MANUAL は 0 です
これを使うべきです
if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
//Automatic mode
} else {
//Manual mode
}
これの代わりに
if (mode == 1) {
//Automatic mode
} else {
//Manual mode
}
明るさを手動で変更できるようになりました
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness); //brightness is an integer variable (0-255), but dont use 0
そして明るさを読む
try {
int brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); //returns integer value 0-255
} catch (Exception e) {}
これですべてが適切に設定されましたが、まだ変化は見られません。変化を確認するには、もう 1 つ必要です。画面を更新します...次のようにします。
try {
int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); //this will get the information you have just set...
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = (float) br / 255; //...and put it here
getWindow().setAttributes(lp);
} catch (Exception e) {}
許可を忘れないでください...
<uses-permission android:name="android.permission.WRITE_SETTINGS" />