当面部在 Android 上关闭屏幕时以编程方式关闭屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21884181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:15:36  来源:igfitidea点击:

Turn off screen programmatically when face is close the screen on Android

androidscreenandroid-sensorssensormanager

提问by Solkin

My app is a dialer and when user holding the phone near his head I need to turn screen off and prevent clicking on the controls - like native Android dialer behavior. What API level I need and how can I do this in right way?

我的应用程序是一个拨号器,当用户将手机靠近他的头时,我需要关闭屏幕并防止点击控件 - 就像原生 Android 拨号器行为一样。我需要什么 API 级别以及如何以正确的方式执行此操作?

回答by Solkin

I found solution by disassembling one very famous VoIP application. This activity after pressing button1 will disable screen and hardware keys when you close sensors. After pressing button2 this function will be switched off.

我通过拆卸一个非常著名的 VoIP 应用程序找到了解决方案。当您关闭传感器时,按下 button1 后的此活动将禁用屏幕和硬件键。按下按钮 2 后,此功能将关闭。

Also, this function required permission:

此外,此功能需要权限:

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

Activity. Try it.

活动。尝试一下。

public class MainActivity extends Activity {

    private Button button1;
    private Button button2;
    private PowerManager powerManager;
    private PowerManager.WakeLock wakeLock;
    private int field = 0x00000020;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            // Yeah, this is hidden field.
            field = PowerManager.class.getClass().getField("PROXIMITY_SCREEN_OFF_WAKE_LOCK").getInt(null);
        } catch (Throwable ignored) {
        }

        powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(field, getLocalClassName());

        setContentView(R.layout.main);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);

        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(!wakeLock.isHeld()) {
                    wakeLock.acquire();
                }
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(wakeLock.isHeld()) {
                    wakeLock.release();
                }
            }
        });
    }
}

回答by i.n.e.f

The following code shows you how to use the proximity sensor:

以下代码向您展示了如何使用接近传感器:

public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mProximity;

 @Override
 public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Get an instance of the sensor service, and use that to get an instance of
// a particular sensor.
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
 }

 @Override
 public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
 }

 @Override
  public final void onSensorChanged(SensorEvent event) {
   float distance = event.values[0];
// Do something with this sensor data.
   }

 @Override
 protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
 }

 @Override
  protected void onPause() {
// Be sure to unregister the sensor when the activity pauses.
super.onPause();
mSensorManager.unregisterListener(this);
 }}

try thislink for the use of Proximity Sensor while Face is close to screen turn off the screen.

尝试链接以在面部靠近屏幕时使用接近传感器关闭屏幕。

Hope this Helps you.

希望这对你有帮助。

回答by justingregoryuk

There are lot of devices where the CPU usage goes high if you try the method posted by Solkin (using the wakelock). This is a bug in android devices.

如果您尝试使用 Solkin 发布的方法(使用唤醒锁),有很多设备的 CPU 使用率会很高。这是安卓设备中的一个错误。

You should use the onSensorChanged event and just display a black screen to avoid any issues.

您应该使用 onSensorChanged 事件并只显示黑屏以避免任何问题。