Android Question The code that controls the bright screen and the information screen can be changed to B4A?

rachenjian

Member
Licensed User
C#:
//Bright screen logic code

private PowerManager powerManager;

private PowerManager.WakeLock wakeLock;

public void openScreenOn() {

if (powerManager == null) {

            powerManager = (PowerManager) AppApplication.getApplicationContexts().getSystemService(Context.POWER_SERVICE);

        }

if (wakeLock == null) {

            wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG");

        }

boolean ifOpen = powerManager.isScreenOn();

if (!ifOpen) {

            //The screen lights up continuously

            wakeLock.acquire();

            //Release the lock so that the screen turns off after 2 minutes

            wakeLock.release();

        }

}



//The logic that needs to be called to the screen is called by the code

 boolean root = AndroidRootUtils.checkDeviceRoot();

        if(root){

            AndroidRootUtils.execRootCmd("input keyevent 224");

        }





//AndroidRootUtils   Root permission tool class

public class AndroidRootUtils {

    /**

     *

     * Determine whether the machine Android is rooted, that is, whether to obtain root permissions

     */

    public static boolean checkDeviceRoot() {

        boolean resualt = false;

            int ret = execRootCmdSilent("echo test"); // 通过执行测试命令来检测

            if (ret != -1) {

                Log.i("checkDeviceRoot", "this Device have root!");

                resualt = true;

            } else {

                resualt = false;

                Log.i("checkDeviceRoot", "this Device not root!");

            }

        return resualt;

    }



    /**

     * The command is executed and the result is output

     */

    public static String execRootCmd(String cmd) {

        String result = "";

        DataOutputStream dos = null;

        DataInputStream dis = null;

        try {

            Process p = Runtime.getRuntime().exec("su");//The android system that has been rooted has the su command

            dos = new DataOutputStream(p.getOutputStream());

            dis = new DataInputStream(p.getInputStream());

            dos.writeBytes(cmd + "\n");

            dos.flush();

            dos.writeBytes("exit\n");

            dos.flush();

            String line = null;

            while ((line = dis.readLine()) != null) {

                result += line;

            }

            p.waitFor();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if (dos != null) {

                try {

                    dos.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            if (dis != null) {

                try {

                    dis.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

        }

        return result;

    }



    /**

     * Execute commands without paying attention to the resulting output

     */

    public static int execRootCmdSilent(String cmd) {

        int result = -1;

        DataOutputStream dos = null;

        try {

            Process p = Runtime.getRuntime().exec("su");

            dos = new DataOutputStream(p.getOutputStream());

            dos.writeBytes(cmd + "\n");

            dos.flush();

            dos.writeBytes("exit\n");

            dos.flush();

            p.waitFor();

            result = p.exitValue();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if (dos != null) {

                try {

                    dos.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

        }

        return result;

    }

}
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top