19

我想在我的项目中使用接近传感器。我搜索了接近传感器教程。我在http://www.vogella.com/articles/AndroidSensor/article.html#sensoroverview_manager找到了传感器教程。我尝试使用接近传感器作为以下代码:

@Override
public void onSensorChanged(SensorEvent event)
{
    if(event.sensor.getType() == Sensor.TYPE_PROXIMITY)
    {
        Toast.makeText(getApplicationContext(), "working", Toast.LENGTH_SHORT).show();
    }
}

Activity正在实施SensorEventListener. 但它不起作用。我是否需要使用任何权限才能使用接近传感器?或者我的代码做这些事情是错误的。您的帮助将不胜感激。谢谢

4

4 回答 4

27

我使用这段代码:

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager mSensorManager;
    private Sensor mProximity;
    private static final int SENSOR_SENSITIVITY = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
            if (event.values[0] >= -SENSOR_SENSITIVITY && event.values[0] <= SENSOR_SENSITIVITY) {
                //near
                Toast.makeText(getApplicationContext(), "near", Toast.LENGTH_SHORT).show();
            } else {
                //far
                Toast.makeText(getApplicationContext(), "far", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

}

您也可以在 GitHub 中查看此代码

于 2016-03-02T09:30:25.983 回答
7

使用接近传感器的正确方法是通过 PowerManager。忘记光传感器,使用 Android 抽象和已经编写的代码!

public class ProximityViaPowerManager {

private Context context;
private WakeLock proximityWakeLock;

public ProximityViaPowerManager(Context context) {
    this.context = context;
}

public boolean enableProximityWakeLock() {
    if (proximityWakeLock != null) {
      return true;
    }
    PowerManager powerManager = 
 context.getApplicationContext().getSystemService(Context.POWER_SERVICE);
    proximityWakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
    proximityWakeLock.acquire();
    return true;
  }

  public void disableProximityWakeLock() {
    if (proximityWakeLock != null) {
      proximityWakeLock.release();
      proximityWakeLock = null;
    }
  }
}

确保在 Android 生命周期恢复/暂停中正确启用/禁用功能,并将权限添加到清单:

<uses-permission android:name="android.permission.WAKE_LOCK" />
于 2018-04-09T20:09:37.947 回答
4

你要注册你的听众。在您的活动中:

@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
protected void onResume() {
    // Register a listener for the sensor.
    super.onResume();
    mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
}

然后在你重写的 onSensorChanged() 方法中,你可以用传感器数据做你的事情。

抱歉,如果我的回答为时已晚 =)

于 2015-08-06T09:14:57.027 回答
2

您可以遵循此代码,该代码具有我们可以对任何传感器进行编程的基本框架。

更改此行

linearAcceleration = senseManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

proximity = senseManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
于 2017-01-29T16:21:40.660 回答