Failed to get proximity sensor values ​​in android

Can someone provide an example of using a proximity sensor? I tried to use it in the same way as other sensors, but it does not work.

This is the code snippet I used:

 final SensorManager mSensorManager;
 final Sensor mproximity;

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

mSensorManager.registerListener(new SensorListener(){

public void onAccuracyChanged(int arg0, int arg1) {
   // TODO Auto-generated method stub
   Toast.makeText(test.this,"proximity sensor accu ", Toast.LENGTH_SHORT).show();
}

public void onSensorChanged(int arg0, float[] arg1) {
   // TODO Auto-generated method stub
   Toast.makeText(test.this,"proximity sensor ", Toast.LENGTH_SHORT).show();
}

}, Sensor.TYPE_PROXIMITY, 1);

Please tell me where I am wrong.

+5
source share
2 answers

Method registerListener(SensorListener, int, int)deprecated, use registerListener(SensorEventListener, Sensor, int)instead:

mSensorManager.registerListener(proximityListener,
        mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),
        SensorManager.SENSOR_DELAY_UI);

In addition, you must save the link to your Sensor(Event)Listenerin order to be able to cancel the registration.

+5
source

Gubbel makes the right point. Use the latest API.

, , . "" .

, onSensorChanged , (.. ).

.
, /
. (0/1 /) .

Android.

+2

All Articles