How to control Android proximity sensor?

2020-06-12 04:18发布

问题:

Can someone provide an example as to how to use the proximity sensor? Please describe some event and how to use them.

回答1:

Every android mobile is shipped with sensors to measure various environmental conditions.Proximity sensor measures the distance that some object is from the device. It is often used to detect the presence of a person’s face next to the device.

Some proximity sensors only support a binary near or far measurement. In this case, the sensor should report its maximum range value in the far state and a lesser value in the near state.

package com.exercise.AndroidProximitySensor;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidProximitySensorActivity extends Activity {
    /** Called when the activity is first created. */

 TextView ProximitySensor, ProximityMax, ProximityReading;

 SensorManager mySensorManager;
 Sensor myProximitySensor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ProximitySensor = (TextView)findViewById(R.id.proximitySensor);
        ProximityMax = (TextView)findViewById(R.id.proximityMax);
        ProximityReading = (TextView)findViewById(R.id.proximityReading);

        mySensorManager = (SensorManager)getSystemService(
          Context.SENSOR_SERVICE);
        myProximitySensor = mySensorManager.getDefaultSensor(
          Sensor.TYPE_PROXIMITY);

        if (myProximitySensor == null){
         ProximitySensor.setText("No Proximity Sensor!"); 
        }else{
         ProximitySensor.setText(myProximitySensor.getName());
         ProximityMax.setText("Maximum Range: "
           + String.valueOf(myProximitySensor.getMaximumRange()));
         mySensorManager.registerListener(proximitySensorEventListener,
           myProximitySensor,
           SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    SensorEventListener proximitySensorEventListener
    = new SensorEventListener(){

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
   // TODO Auto-generated method stub

  }

  @Override
  public void onSensorChanged(SensorEvent event) {
   // TODO Auto-generated method stub

   if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
    ProximityReading.setText("Proximity Sensor Reading:"
      + String.valueOf(event.values[0]));
   }
  }
    };
}

The layout xml for the above code is given below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<TextView  
    android:id="@+id/proximitySensor"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<TextView  
    android:id="@+id/proximityMax"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<TextView  
    android:id="@+id/proximityReading"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

In the above example we have 3 textview's. The first one is to display if the device supports proximity sensor, the second to display the maximum range supported by the sensor and the third one to display the current reading.

Hope this has given you a basic idea about proximity sensors.