亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以編程方式更改屏幕亮度(與電源小部件一樣)

以編程方式更改屏幕亮度(與電源小部件一樣)

呼啦一陣風 2019-08-19 15:32:32
以編程方式更改屏幕亮度(與電源小部件一樣)我正在搜索如何以編程方式更改屏幕的亮度,我發現這是一個非常好的解決方案,它工作得很好,但它只在我的應用程序處于活動狀態時才有效。我的應用程序關閉后,亮度返回與我啟動應用程序之前相同的值。我希望能夠改變亮度就像我從電源小部件按下亮度按鈕一樣。在來自android的電源小部件中有3個狀態。一個非常明亮的一個非常黑暗,一個介于兩者之間 是否有可能像有人按下這個小部件一樣改變亮度?編輯1:我創建了這個,我添加了permision到我的清單但是當應用程序啟動時我沒有看到任何亮度的變化,我嘗試用10和100現在有200但沒有任何改變任何建議?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);}
查看完整描述

3 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

這是我發現的完整代碼:

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));

我的問題中的代碼不起作用,因為屏幕沒有刷新。因此,刷新屏幕的一個方法是啟動虛擬活動,而不是在創建虛擬活動時調用,finish()以便亮度的更改生效。


查看完整回答
反對 回復 2019-08-19
?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

在該鏈接中使用Tor-Morten的解決方案,但也設置系統亮度設置如下:

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

其中bright整數范圍是1到255。


查看完整回答
反對 回復 2019-08-19
?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

我今天解決了這個問題。

首先,您必須將權限放入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 onSettings.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) {}

現在一切都設置得很好,但是......你還看不到變化。你還需要一件事才能看到變化!刷新屏幕......所以這樣做:

    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" />


查看完整回答
反對 回復 2019-08-19
  • 3 回答
  • 0 關注
  • 496 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號