No object Near
Near Object (near Ear while calling)
Create new Android Project
Project Name: ProximitySensor
//tested from 2.3.3 to current android sdk
Build Target: Android 2.3.3 //or greater than that
Application Name: Proximity Sensor
Package Name: com.shaikhhamadali.blogspot.proximitysensor
Create layout file: activity_proximity_sensor
Project Name: ProximitySensor
//tested from 2.3.3 to current android sdk
Build Target: Android 2.3.3 //or greater than that
Application Name: Proximity Sensor
Package Name: com.shaikhhamadali.blogspot.proximitysensor
Create layout file: activity_proximity_sensor
1.create layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ProximitySensor" > <TextView android:id="@+id/TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Proximity Sensor" android:textColor="#357345" android:textSize="30sp" /> <TextView android:id="@+id/tVProximity" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/TextView" android:layout_marginTop="10dp" android:textSize="20sp" /> </RelativeLayout>
2.code of activity:
package com.shaikhhamadali.blogspot.proximitysensor; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.widget.TextView; import android.widget.Toast; public class ProximitySensor extends Activity implements SensorEventListener{ //SensorManager lets you access the device's sensors //declare Variables private SensorManager sensorManager; TextView tVProximity; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_proximity_sensor); tVProximity = (TextView)findViewById(R.id.tVProximity); //create instance of sensor manager and get system service to interact with Sensor sensorManager= (SensorManager)getSystemService(Context.SENSOR_SERVICE); Sensor proximitySensor= sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); if (proximitySensor == null){ Toast.makeText(ProximitySensor.this,"No Proximity Sensor Found! ",Toast.LENGTH_LONG).show(); } } @Override protected void onResume() { super.onResume(); // register this class as a listener for the Proximity Sensor sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY), SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { // unregister listener super.onPause(); sensorManager.unregisterListener(this); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } // called when sensor value have changed @Override public void onSensorChanged(SensorEvent event) { // The Proximity sensor returns a single value either 0 or 5(also 1 depends on Sensor manufacturer). // 0 for near and 5 for far if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){ if(event.values[0]==0){ tVProximity.setText("You are Near: "+String.valueOf(event.values[0])); } else{ tVProximity.setText("You are Far: "+String.valueOf(event.values[0])); } }} }
3. note that:
- you can use TYPE_PROXIMITY sensor to check that the device is near to any object.
- must un register Sensor on pause and register on resume.
- Learn More About Barometer-Pressure sensor
- Learn more about Sensors and multiple types of sensors.
- Learn about how to get device Sensor List.
- Learn about use Of Type Accelerometer Sensor.
4. conclusion:
- Some information about how to use TYPE_PROXIMITY sensor.
- know what are Sensors and how to use Sensors.
- know that Proximity Sensor is used to check that device is near to any object or not.
5. About the post:
- Almost every android mobile is shipped with sensors to measure various environmental conditions.Proximity sensors would be useful to reveal the nearness of an object to the phone. We might often experience that our phone screen would turn off when we bring the phone to our ears when we are in a call and the screen will turn on when we take it back. This is because the proximity sensor recognizes the object near the phone.
- The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
6. Source Code:
you can download the source code here