文章目录
1、开发需求
需求:开发一个Android apk实现手机亮度控制
2、运行环境
Android studio最新版本
3、主要文件
app\src\main\AndroidManifest.xml
app\src\main\res\layout\activity_main.xml
app\src\main\java\com\example\sylon\MainActivity.java
代码路径:app\src\main\AndroidManifest.xml
需要修改权限
4、布局文件信息
路径:app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="274dp"
android:layout_height="32dp"
android:layout_marginStart="27dp"
android:layout_marginTop="38dp"
android:layout_marginEnd="83dp"
android:layout_marginBottom="24dp"
android:maxHeight="5.0dp"
android:minHeight="5.0dp"
android:progressDrawable="@drawable/sb_bar"
android:thumb="@drawable/sb_thumb"
app:layout_constraintBottom_toTopOf="@+id/textview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.037"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="16dp"
android:text="sylonbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.187" />
</androidx.constraintlayout.widget.ConstraintLayout>
5、手机界面控制代码
路径:app\src\main\java\com\example\sylon\MainActivity.java
package com.example.sylon;
import android.os.Bundle;
import android.app.Activity;
import android.view.WindowManager;
import android.view.Window;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.content.ContentResolver;
import android.provider.Settings;
import android.util.Log;
/*
public class MouseSpeedChanger {
private static final String TAG = "MouseSpeedChanger";
// 修改鼠标速度的方法
public static void setMouseSpeed(ContentResolver resolver, int speed) {
if (speed >= 1 && speed <= 10) {
// 将鼠标速度的值存储到系统设置中
Settings.System.putInt(resolver, Settings.System.POINTER_SPEED, speed);
// 通知系统设置已更改
Uri uri = Settings.System.getUriFor(Settings.System.POINTER_SPEED);
resolver.notifyChange(uri, null);
Log.d(TAG, "Mouse speed set to: " + speed);
} else {
Log.e(TAG, "Invalid mouse speed value: " + speed);
}
}
}
*/
public class MainActivity extends AppCompatActivity {
private SeekBar seekb_normal;
private TextView txt_cur;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//mContext = MainActivity.this;
bindViews();
}
// 获取当前屏幕亮度值
private int getCurrentBrightness() {
int brightness = 0;
try {
brightness = Settings.System.getInt(getContentResolver(), Settings.System.POINTER_SPEED);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace(); // 捕获异常
}
return brightness;
}
// 设置新的屏幕亮度
private void setBrightness(int brightness) {
ContentResolver cResolver = getContentResolver();
// 将进度值转换为0-255范围
int newBrightness = brightness * 255 / 100;
if (Settings.System.canWrite(this)) {
Settings.System.putInt(cResolver, Settings.System.POINTER_SPEED, newBrightness);
}
// 更新当前窗口的亮度
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = newBrightness / 255.0f; // 转换为0.0到1.0之间
getWindow().setAttributes(layoutParams);
}
private void bindViews() {
seekb_normal = (SeekBar) findViewById(R.id.seekBar);
txt_cur = (TextView) findViewById(R.id.textview);
int currentBrightness = getCurrentBrightness();
seekb_normal.setProgress(currentBrightness*100/255);
txt_cur.setText("当前亮度: " + currentBrightness*100/255);
mContext = getApplicationContext();
seekb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setBrightness(progress);
txt_cur.setText("当前亮度:" + progress + " / 100 ");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext, "触碰SeekBar", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext, "放开SeekBar", Toast.LENGTH_SHORT).show();
}
});
}
}
6、debug
工具如果找不到,直接全局搜索。