控制系统硬件功能

(1).打开和关闭手电筒

(2).调节屏幕亮度

本部分代码地址:https://github.com/loveyoyo/SimpleDemo/blob/master/app/src/main/java/com/kang/demo/hardware/ScreenBrightnessActivity.java

屏幕亮度的知识点

  • Android系统的屏幕亮度取值范围是0-255,数据是int型;
  • 屏幕亮度的模式有两种,如下:
字段 解释
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL 手动调节模式 0
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC 自动调节模式 1

设置屏幕亮度的常见方法如下

  • 获取当前屏幕亮度:获取到的是0-255范围内的值;
private int getCurrentScreenBrightness(Context context) {
    int currentBrightness = 0;
    ContentResolver contentResolver = context.getContentResolver();
    try {
        currentBrightness = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
    } catch (Settings.SettingNotFoundException e) {
        e.printStackTrace();
    }
    return currentBrightness;
}
  • 获取当前屏幕亮度模式:返回值是0(手动调节模式)或者1(自动调节模式);
private int getCurrentScreenBrightnessMode(Context context) {
    int screenBrightnessMode = 0;
    ContentResolver contentResolver = context.getContentResolver();
    try {
        screenBrightnessMode = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE);
    } catch (Settings.SettingNotFoundException e) {
        e.printStackTrace();
    }
    return screenBrightnessMode;
}
  • 设置屏幕调节模式:需要先获取android.permission.WRITE_SETTINGS这个权限,然后才能设置屏幕调节模式,否则会报错:was not granted this permission: android.permission.WRITE_SETTINGS。0-手动调节模式,1-自动调节模式;
//清单文件中申请权限;
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

//6.0以后需要动态申请系统的权限;
private void requestPermission(int mode) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.System.canWrite(this)) {
            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:" + this.getPackageName()));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.startActivity(intent);
        } else {
            //有了权限,具体的操作;
            setScreenMode(this, mode);
        }
    }
}
    
//具体的操作屏幕亮度;
private void setScreenMode(Context context, int mode) {
    ContentResolver contentResolver = getContentResolver();
    Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
}
  • 设置当前页面的屏幕亮度,但是并不会影响系统的屏幕亮度,也就是说设置了当前页面的屏幕亮度,系统的屏幕亮度还是先前的数值,需要申请权限android.permission.CHANGE_CONFIGURATION,否则不会执行调节屏幕亮度;需要注意的是参数context的类型必须是Activity,不能是Context;用这种方法设置界面的亮度,会使亮度自动调节功能失效,离开该页面,亮度自动调节会继续生效;
////清单文件中申请权限;
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />

private void setCurrentBrightness(Activity activity, int brightness) {
    WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
    layoutParams.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
    activity.getWindow().setAttributes(layoutParams);
}
  • 设置系统的屏幕亮度,直接影响系统的屏幕亮度。和设置亮度模式一样,需要先获取android.permission.WRITE_SETTINGS这个权限,然后才能设置屏幕调节模式,否则会报错:was not granted this permission: android.permission.WRITE_SETTINGS。;
//还是需要在6.0以上的手机上申请android.permission.WRITE_SETTINGS权限;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (!Settings.System.canWrite(this)) {
        Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:" + this.getPackageName()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    } else {
        //有了权限,具体的操作;
        setSystemBrightness(this,200);
    }
}
//具体设置系统的屏幕亮度;                
private void setSystemBrightness(Context context,int brightness){
    ContentResolver contentResolver = context.getContentResolver();
    Uri uri = android.provider.Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
    android.provider.Settings.System.putInt(contentResolver,Settings.System.SCREEN_BRIGHTNESS,brightness);
    contentResolver.notifyChange(uri,null);
}                
  • 设置屏幕常亮,有三种方法如下;

(1).在需要常亮的页面,设置跟布局或者页面中的某个控件的属性android:keepScreenOn="true",就可以实现了,也可以代码动态控制,mView.setKeepScreenOn(boolean isOn);

(2).在需要常亮的页面,添加以下代码就实现了;

getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

(3).需要先在清单文件中添加以下权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />

然后在需要常亮的页面,添加常亮的方法:

private void setScreenOn() {
    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "kang");
    wakeLock.acquire();
    }
  • 设置屏幕熄屏,两种方法如下:
    (1).和设置屏幕常亮时的第三种方法对应,使用wakeLock.release()方法,但是这样操作之后,屏幕只是取消了常亮设置,还会在你原先设置的系统屏幕无操作的熄屏时间之后才会熄屏;而且这种方法需要在使用了wakeLock.acquire()方法之后才能调用;
    (2).需要添加设置管理器;
    首先是需要的一个类如AminCustomReceiver继承自DeviceAdminReceiver,DeviceAdminReceiver是一个BroadcastReceiver;在清单文件中配置AminCustomReceiver,代码如下:
<receiver android:name=".hardware.AdminCustomReceiver"
    android:permission="android.permission.BIND_DEVICE_ADMIN">
    <meta-data android:name="android.app.device_admin"
        android:resource="@xml/device_admin"
    />
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
    </intent-filter>
</receiver>

然后,在res文件夹下建立xml文件夹,在xml文件夹下创建一个在清单文件中用到的device_admin.xml文件,其中代码如下:

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <force-lock/>
    </uses-policies>
</device-admin>

最后,才AdminCustomReceiver代码中添加方法:

private void setScreenOff() {
    DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    ComponentName componentName = new ComponentName(this, AdminReceiver.class);
    boolean isAdmin = devicePolicyManager.isAdminActive(componentName);
    Log.e("===MockingJay===", "isAdmin:" + isAdmin);
    if (!isAdmin) {
        //打开DevicePolicyManager管理器,授权页面
        Intent intent = new Intent();
        //授权页面Action -->  DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN
            intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        //设置DEVICE_ADMIN,告诉系统申请管理者权限的Component/DeviceAdminReceiver
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        //设置 提示语--可不添加
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "DevicePolicyManager涉及的管理权限,一次性激活!");
        startActivityForResult(intent, 100);
    } else {
        //已经添加到设置管理器了;
    }
    devicePolicyManager.lockNow();
}

(3).控制手机音量

(4).