Android--java实现手机亮度控制

文章目录

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

工具如果找不到,直接全局搜索。

相关推荐
枣伊吕波10 分钟前
第六节第二部分:抽象类的应用-模板方法设计模式
android·java·设计模式
萧然CS13 分钟前
使用ADB命令操作Android的apk/aab包
android·adb
xinxiyinhe13 分钟前
内存泄漏与OOM崩溃根治方案:JVM与原生内存池差异化排查手册
java·开发语言·jvm
心向阳光的天域17 分钟前
黑马Java跟学.最新AI+若依框架项目开发(一)
java
what_201822 分钟前
分布式链路跟踪
java·运维·分布式
oliveira-time30 分钟前
ArrayList和LinkedList区别
java·开发语言
潮流coder33 分钟前
IntelliJ IDEA给Controller、Service、Mapper不同文件设置不同的文件头注释模板、Velocity模板引擎
java·ide·intellij-idea
AORO_BEIDOU38 分钟前
防爆手机与普通手机有什么区别
人工智能·5g·安全·智能手机·信息与通信
码农飞哥40 分钟前
互联网大厂Java求职面试实战:Spring Boot与微服务场景深度解析
java·数据库·spring boot·安全·微服务·消息队列·互联网医疗
Akiiiira1 小时前
【日撸 Java 300行】Day 14(栈)
java·开发语言