0% found this document useful (0 votes)
19 views3 pages

Tempreture Sensor App

Uploaded by

vivek koli
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)
19 views3 pages

Tempreture Sensor App

Uploaded by

vivek koli
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
You are on page 1/ 3

**activity_main.

xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tempreture Sensor"
android:textAlignment="center"
android:textSize="30dp"/>

<TextView
android:id="@+id/tvTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Tempreture=" />

</LinearLayout>

**MainActivity.java

package com.example.tempsensorapp;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements SensorEventListener {


TextView tvTemp;
SensorManager sMgr;
Sensor sTemp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTemp=findViewById(R.id.tvTemp);
sMgr= (SensorManager) getSystemService(SENSOR_SERVICE);
sTemp=sMgr.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
}

@Override
protected void onPause() {
super.onPause();
sMgr.unregisterListener(this,sTemp);

@Override
protected void onResume() {
super.onResume();
sMgr.registerListener(this,sTemp,SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if(sensorEvent.sensor.getType()==Sensor.TYPE_AMBIENT_TEMPERATURE)
{
tvTemp.setText("Tempreture="+sensorEvent.values[0]+"°C");
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}
}

**output

You might also like