Modification of system settings for Android all-in-one development——automatically lock the screen

insert image description here
Modify system settings for Android all-in-one research and development——screen brightness

Modification of system settings for Android all-in-one development———sound

Modification of system settings for Android all-in-one development——automatically lock the screen

The series of chapters on modifying system settings will open soon! This chapter will explain the automatic lock screen in detail.

The automatic screen lock function , this can be based on the needs of users, in order to reduce the power consumption and performance of the all-in-one machine, you can 设置具体的锁屏时间或者设置永不锁屏wait.

auto lock screen

Automatically lock the screen, that is, set the backlight time , 毫秒and set it in units.

1= 601= 1000毫秒

//15秒   15000
//30秒   30000
//1分钟  60000
//2分钟  120000
//3分钟  180000
//4分钟  240000
//5分钟  300000
//6分钟  360000
//7分钟  420000
//8分钟  480000
//9分钟  540000
//10分钟 600000
//永不  2147483647 = 35791分 = 24天

Design ideas

  1. AndroidManifest setting permissions;

  2. Get the system lock screen time;

  3. Modify the backlight time in the system Settings;

    3.1 Judgment authority

    3.2 Modify the backlight time if necessary

    3.3 Guide the user to authorize if there is none

Implementation

AndroidManifest setting permissions
	<!-- 修改系统设置 -->
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="android.permission.HARDWARE_TEST" />

Get system lock screen time
	int screenOffTime = getScreenOffTime();
    Log.e("当前锁屏时间 >>>", "init: " + screenOffTime);
    if (screenOffTime < 50000) {
    
    
            String second = AppUtils.msToS(screenOffTime);
            timeTxt.setText(second + "秒");
    } else {
    
    
        String minute = AppUtils.msToM(screenOffTime);
        if (minute.equals("35791")) {
    
    
            timeTxt.setText("永不");
       } else {
    
    
            timeTxt.setText(minute + "分");
      }
   }
	/**
     * 获得锁屏时间  毫秒
     */
    private int getScreenOffTime() {
    
    
        int screenOffTime = 0;
        try {
    
    
            screenOffTime = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
        } catch (Exception localException) {
    
    
        }
        return screenOffTime;
    }
	/**
     * 毫秒转秒
     * @param ms
     * @return
     */
    public static String msToS(int ms) {
    
    
        int seconds = ms / 1000;
        seconds = seconds % 60;

        String s = null;

        if (seconds == 0)
            seconds = 1;

        if (seconds < 10)
            s = "" + seconds;
        else
            s = "" + seconds;

        return s;
    }

    /**
     * 毫秒转分
     * @param ms
     * @return
     */
    public static String msToM(int ms) {
    
    
        int seconds = ms / 1000;
        int minutes = seconds / 60;
        seconds = seconds % 60;

        String m = null;

        if (minutes == 0 && seconds == 0)
            seconds = 1;

        if (minutes < 10)
            m = "" + minutes;
        else
            m = "" + minutes;

        return m;
    }

Modify the backlight time in the system Settings
	/**
     * 非系统签名应用,引导用户手动授权修改Settings 权限
     **/
    private int dousingCode = 2;
    
	rela.setOnClickListener(new View.OnClickListener() {
    
    
            (api = Build.VERSION_CODES.M)
            
            public void onClick(View v) {
    
    
                if (Settings.System.canWrite(SoundAndBrightActivity.this)) {
    
    
                    //设置背光时间
                    showDousingTime();
                } else {
    
    
                    Toast.makeText(SoundAndBrightActivity.this, "没有修改权限", Toast.LENGTH_SHORT).show();

                    // 打开允许修改Setting 权限的界面
                    Intent intent = new Intent(
                            Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri
                            .parse("package:"
                                    + getPackageName()));
                    startActivityForResult(intent,
                            dousingCode);
                }
            }
        });
    }

User is authorized

	/**
     * 设置背光时间  毫秒
     */
    private void setScreenOffTime(int paramInt) {
    
    
        try {
    
    
            Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, paramInt);
        } catch (Exception localException) {
    
    
            localException.printStackTrace();
        }
    }

User is not authorized

In case of unauthorized access, guide the user to授权

Toast.makeText(SoundAndBrightActivity.this, "没有修改权限", Toast.LENGTH_SHORT).show();

// 打开允许修改Setting 权限的界面
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri
                            .parse("package:"
                                    + getPackageName()));
                    startActivityForResult(intent,
                            dousingCode);

At the same time, it is enough to detect and return the result and process it.

	(api = Build.VERSION_CODES.M)
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == dousingCode) {
    
    
            if (Settings.System.canWrite((Context) SoundAndBrightActivity.this)) {
    
    
                 //设置背光时间
                showSelectTime();
            } else {
    
    
                Toast.makeText((Context) this, "您已拒绝修系统Setting的自动熄屏权限", Toast.LENGTH_SHORT).show();
            }
        }
    }


Recently, I took a break from my busy schedule, ↓↓↓↓[Who’s De Code Farmer Mr. Chen]↓↓↓↓, I will share technical blog posts and high-energy information content with you regularly! Welcome all bosses to like and follow, you are my source of motivation!

Guess you like

Origin blog.csdn.net/chen_md/article/details/129007320