如何在Android中以程式設計方式更改螢幕亮度?


此示例演示瞭如何在Android中以程式設計方式更改螢幕亮度。

步驟 1 - 在Android Studio中建立一個新專案,轉到檔案⇒新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟 2 - 將以下程式碼新增到res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://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="Turn Off Screen"/>
   <SeekBar
      android:id="@+id/seekBar"
      android:layout_marginTop="10dp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
</LinearLayout>

步驟 3 - 將以下程式碼新增到src/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();
      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

<?xml version="1.0" encoding="utf-8"?>
<manifest
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   package="app.com.sample">
   <uses-permission
      android:name="android.permission.WRITE_SETTINGS"
      tools:ignore="ProtectedPermissions" />
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action
               android:name="android.intent.action.MAIN" />
            <category
               android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟您的專案活動檔案之一,然後單擊工具欄中的執行播放圖示 圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

更新於: 2020年7月2日

3K+ 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.