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

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

相关推荐
自律的kkk26 分钟前
mysql基础快速入门
java·数据库·mysql·oracle
alien爱吃蛋挞27 分钟前
List详解
java·list
HackKong33 分钟前
高校网络安全_网络安全之道
java·网络·c++·python·学习·web安全·黑客技术
Andy01_1 小时前
Java八股汇总【MySQL】
java·开发语言·mysql
唐 城1 小时前
Solon v3.0.5 发布!(Spring 可以退休了吗?)
java·spring·log4j
V+zmm101341 小时前
社区二手物品交易小程序ssm+论文源码调试讲解
java·微信小程序·小程序·毕业设计·ssm
坊钰1 小时前
【Java 数据结构】合并两个有序链表
java·开发语言·数据结构·学习·链表
秋天下着雨1 小时前
apifox调用jar程序
java·python·jar
m0_748251081 小时前
docker安装nginx,docker部署vue前端,以及docker部署java的jar部署
java·前端·docker
A22741 小时前
Redis——缓存雪崩
java·redis·缓存