22 Sensor
22 Sensor
>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light Sensor"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_centerInParent="true" />
</RelativeLayout>
package com.mrtechy.gfg_sensor
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatDelegate
class MainActivity : AppCompatActivity(), SensorEventListener {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
text = findViewById(R.id.tv_text)
// setupSensor Called
setUpSensor()
brightness = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)
if (event?.sensor?.type == Sensor.TYPE_LIGHT) {
val light1 = event.values[0]
return
super.onResume()
sensorManager.registerListener(this, brightness,
SensorManager.SENSOR_DELAY_NORMAL)
sensorManager.unregisterListener(this)