Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using proximity sensor in android

I want to use proximity sensor in my project. I searched for proximity sensor tutorial. And I found a sensor tutorial at http://www.vogella.com/articles/AndroidSensor/article.html#sensoroverview_manager. I tried to use proximity sensor as following code:

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

My Activity is implementing SensorEventListener. But its not working. Do I need to use any permission to use proximity sensor? Or My code is wrong for doing this stuff. Your help will be very appreciated. Thank you

like image 498
Sanjay Joshi Avatar asked Jul 31 '13 10:07

Sanjay Joshi


People also ask

What is the use of proximity sensor in Android?

The proximity sensor detects when a user is holding the phone near their face during a call and turns off the display to prevent keypad presses and battery consumption from the display. The proximity/light sensor is located to the right of the earpiece.

How do I turn on proximity sensor on Android?

Android 6.0, 7.0, 7.1 or 7.1.From your Home screen, tap the Application screen icon. Find and tap Settings → About Phone → Support. If this is the first time you start Support, tap ACCEPT to accept the privacy policy. On the Test tab, scroll down and tap Ear proximity, and follow the on-screen instructions.

How do I know if my proximity sensor is working Android?

One way to test your sensor is by simply placing your phone near your face to see if it lights up. Another way to do so is to request a call and place your phone near your ear to see if it picks up. If these don't work, you can download a free third-party app like Sensor Test to see if it's working.

Where is the proximity sensor on Android?

The proximity sensor is located at the top of your phone and if you hold the screen at a slight angle, you can see the sensors. They look like small holes covered by the screen glass and are close to the speaker.


1 Answers

I use this code:

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) {

    }

}

Also you can check this code in GitHub

like image 110
Cabezas Avatar answered Sep 24 '22 15:09

Cabezas