30

画面の明るさをプログラムで変更する方法を探していましたが、これは非常に優れたソリューションであり、うまく機能することがわかりましたが、アプリがアクティブな間だけ機能しますアプリケーションがシャットダウンされた後、明るさはアプリを起動する前と同じ値に戻ります。

電源ウィジェットから明るさボタンを押したときと同じように、明るさを変更できるようにしたいです。Android の電源ウィジェットには 3 つの状態があります。1 つは非常に明るい、1 つは非常に暗い、もう 1 つはその中間です。このウィジェットを押したときのように、明るさを変えることはできますか?

ここに画像の説明を入力

Edit1:これを作成し、マニフェストに権限を追加しましたが、アプリを起動しても明るさに変化が見られません。

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.provider.Settings.System.putInt(this.getContentResolver(),
        android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);
}
4

5 に答える 5

40

これは、機能していることがわかった完全なコードです。

Settings.System.putInt(this.getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS, 20);

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness =0.2f;// 100 / 100.0f;
getWindow().setAttributes(lp);

startActivity(new Intent(this,RefreshScreen.class));

画面が更新されないため、私の質問のコードは機能しません。したがって、画面をリフレッシュするための 1 つのハックは、ダミー アクティビティを開始することであり、そのダミー アクティビティを作成して呼び出しfinish()て、明るさの変更が有効になるようにすることです。

于 2011-10-05T08:04:36.120 に答える
13

そのリンクで Tor-Morten のソリューションを使用しますが、システムの明るさ設定も次のように設定します。

android.provider.Settings.System.putInt(getContext().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);

は 1 ~ 255brightの範囲の整数です。

于 2011-10-04T11:08:21.760 に答える
10

今日、この問題を解決しました。

最初に 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" />
于 2016-03-15T02:23:33.133 に答える
8

パラメータの設定中にアクティビティのコンテキストを渡すと、別のアクティビティを開始する必要なくジョブが完了します。以下は私のために働いた -

WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.screenBrightness =0.00001f;// i needed to dim the display
this.getWindow().setAttributes(lp);

onSensorChanged()メソッド内にこのコードがあり、トリガーされるたびにディスプレイを暗くしました。

于 2014-05-21T14:40:08.497 に答える
4

複雑な例:

    try {
        //sets manual mode and brightnes 255
        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)
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255);  //this will set the brightness to maximum (255)

        //refreshes the screen
        int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = (float) br / 255;
        getWindow().setAttributes(lp);

    } catch (Exception e) {}
于 2016-03-15T02:34:18.733 に答える