打开屏幕上,并关闭在编程机器人 [英] Turn screen on and off programatically in android

查看:166
本文介绍了打开屏幕上,并关闭在编程机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据接近传感器打开屏幕开和关。我能够关闭屏幕。但code到屏幕背面是行不通的。任何人都可以帮助我吗?
这是code:`

 公共无效onSensorChanged(SensorEvent事件){
如果(event.values​​ [0] == 0){Toast.makeText(getApplicationContext(),传感器中0,Toast.LENGTH_LONG).show();
WindowManager.LayoutParams PARAMS = getWindow()的getAttributes()。
params.flags | = LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
。getWindow()setAttributes(PARAMS);
      }其他{Toast.makeText(getApplicationContext(),传感器1,Toast.LENGTH_LONG).show();
WindowManager.LayoutParams PARAMS = getWindow()的getAttributes()。params.screenBrightness = -1;
。getWindow()setAttributes(PARAMS);
      }
}`


解决方案

首先,我调暗屏幕亮度尽可能低的水平,然后作出的所有GUI元素不可点击触摸的问题。以下是我的code:

  @覆盖
公共无效onSensorChanged(SensorEvent事件){
    // TODO自动生成方法存根
    WindowManager.LayoutParams PARAMS = this.getWindow()的getAttributes()。    如果(event.values​​ [0] == 0){
        // TODO商店原来的亮度值
        params.screenBrightness = 0.005f;
        。this.getWindow()setAttributes(PARAMS);
        enableDisableViewGroup((ViewGroup中)findViewById(R.id.YOUR_MAIN_LAYOUT).getParent(),FALSE);
        Log.e(onSensorChanged,近);    }其他{
        // TODO商店原来的亮度值
        params.screenBrightness = -1.0F;
        。this.getWindow()setAttributes(PARAMS);
        enableDisableViewGroup((ViewGroup中)findViewById(R.id.YOUR_MAIN_LAYOUT).getParent(),TRUE);
        Log.e(onSensorChanged,远);
    }
}

从这里,我参考了禁用触控对于整个屏幕的看法。

I want to turn screen ON and OFF based on the proximity sensor. I am able to turn the screen off. but the code to ON the screen back is not working. Can anyone help me please? This is the code:`

public void onSensorChanged(SensorEvent event) {
if (event.values[0] == 0) {

Toast.makeText(getApplicationContext(), "sensor in 0",Toast.LENGTH_LONG).show();
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);


      } else {

Toast.makeText(getApplicationContext(), "sensor in 1",Toast.LENGTH_LONG).show();
WindowManager.LayoutParams params = getWindow().getAttributes();

params.screenBrightness = -1;
getWindow().setAttributes(params);
      } 
}`

解决方案

First I dimmed screen brightness as low as possible and then made all GUI elements unclickable for touch issues. Following is my code:

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub  
    WindowManager.LayoutParams params = this.getWindow().getAttributes();

    if (event.values[0] == 0) {
        //TODO Store original brightness value
        params.screenBrightness = 0.005f;
        this.getWindow().setAttributes(params);
        enableDisableViewGroup((ViewGroup)findViewById(R.id.YOUR_MAIN_LAYOUT).getParent(),false);
        Log.e("onSensorChanged","NEAR");

    } else {
        //TODO Store original brightness value          
        params.screenBrightness = -1.0f;
        this.getWindow().setAttributes(params);                     
        enableDisableViewGroup((ViewGroup)findViewById(R.id.YOUR_MAIN_LAYOUT).getParent(),true);
        Log.e("onSensorChanged","FAR");  
    }       
}  

From here, I took reference to disable touch for entire screen's view.

这篇关于打开屏幕上,并关闭在编程机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆