Android从熄屏唤醒屏幕

方案一、使用Android唤醒aip

1、加入权限

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

2、进行初始化

  /**
     * 唤醒
     * 屏幕
     */
    private PowerManager pm;// init powerManager
    private Context cnt;
 

private void wakeUpScreen() {

        if (cnt != null) {

            pm = (PowerManager) cnt.getSystemService(POWER_SERVICE);
            mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
                    PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK  |
                    PowerManager.ON_AFTER_RELEASE, "target"); //  this target for tell OS which app control screen

        }

    }

3、强制亮屏

 mWakelock.acquire(1000L);

                       

4、取消强制亮屏,释放控制

 mWakelock.release();

这里需要注意的是acquire和release必须成对使用

常亮方法

1、推荐使用,此法最为简单,无需修改代码

android:keepScreenOn="true" 

只要是控件基本都有这个属性,代码同样也可以设置

2、在程序中用代码实现。代码如下:

把这段代码加在setContentView(R.layout.main)之前即可,这种方法,安装时,不会提示安装人是否允许使用禁止休眠功能

想要那个界面保持长亮就把这句话添加到那个界面类中,没添加此语句的界面类不会保持长亮。最实用的的方法

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


方案二、使用代码点击应用屏幕(app退出无效)

public class AutoTouch {

    public int width = 0;
    public int height = 0;

    /**
     * 传入在屏幕中的比例位置,坐标左上角为基准
     *
     * @param act    传入Activity对象
     * @param ratioX 需要点击的x坐标在屏幕中的比例位置
     * @param ratioY 需要点击的y坐标在屏幕中的比例位置
     */
    public void autoClickRatio(Activity act, final double ratioX, final double ratioY) {
        width = act.getWindowManager().getDefaultDisplay().getWidth();
        height = act.getWindowManager().getDefaultDisplay().getHeight();
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 线程睡眠0.1s
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 生成点击坐标
                int x = (int) (width * ratioX);
                int y = (int) (height * ratioY);

                // 利用ProcessBuilder执行shell命令
                String[] order = {"input", "tap", "" + x, "" + y};
                try {
                    new ProcessBuilder(order).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 传入在屏幕中的坐标,坐标左上角为基准
     *
     * @paramact传入Activity对象
     * @param x   需要点击的x坐标
     * @param y   需要点击的x坐标
     */
    public void autoClickPos(final double x, final double y) {
//        width = act.getWindowManager().getDefaultDisplay().getWidth();
//        height = act.getWindowManager().getDefaultDisplay().getHeight();

        new Thread(new Runnable() {
            @Override
            public void run() {
                // 线程睡眠0.1s
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 利用ProcessBuilder执行shell命令
                String[] order = {"input", "tap", "" + x, "" + y};
                try {
                    new ProcessBuilder(order).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


}

使用

初始化对象 
private static AutoTouch autoTouch = new AutoTouch();//自动点击屏幕


  调用
  autoTouch.autoClickPos( 840, 580);

猜你喜欢

转载自blog.csdn.net/shi450561200/article/details/130849703