0% found this document useful (0 votes)
7 views2 pages

Sensor

The document contains an Android layout file (activity_main.xml) that defines a simple user interface with a TextView displaying the status of a proximity sensor. The MainActivity.java file initializes the sensor and updates the TextView based on the proximity sensor's readings, indicating whether an object is near or far. The code includes a SensorEventListener to handle changes in the sensor's state.

Uploaded by

komalsawant765
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Sensor

The document contains an Android layout file (activity_main.xml) that defines a simple user interface with a TextView displaying the status of a proximity sensor. The MainActivity.java file initializes the sensor and updates the TextView based on the proximity sensor's readings, indicating whether an object is near or far. The code includes a SensorEventListener to handle changes in the sensor's state.

Uploaded by

komalsawant765
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Activity_main.

xml

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

Android:layout_width=”match_parent”

Android:layout_height=”match_parent”

Android:gravity=”center”

Android:orientation=”vertical”>

<TextView

Android:id=”@+id/textView”

Android:text=”Proximity Sensor Status”

Android:textSize=”24sp”

Android:padding=”20dp”

Android:layout_width=”wrap_content”

Android:layout_height=”wrap_content”/>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

SensorManager sensorManager; // SensorManager milavtoy

Sensor proximitySensor; // Proximity sensor define kelay

TextView textView; // Status display karayla TextView

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView); // TextView initialize kelay

// SensorManager system madhun milavtoy

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

// Proximity sensor get karto device madhun

proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

// Listener banavtoy jyane sensor madhe change zale tar kalel

SensorEventListener proximityListener = new SensorEventListener() {

@Override

public void onSensorChanged(SensorEvent event) {

// value choti asel tar object khup near ahe

if (event.values[0] < proximitySensor.getMaximumRange()) {

textView.setText("Object is NEAR");

} else {

textView.setText("Object is FAR");

You might also like