google-developer-training / android-advanced

Solution apps for the apps that students create as they work through the Advanced Android Development training course created by Google Developer Training.
Other
1.14k stars 1.09k forks source link

Advanced Android: [Advanced Android 03.1: Getting sensor data Task 1 #34

Closed Productivix closed 3 years ago

Productivix commented 3 years ago

Describe the problem A clear and concise description of what the problem is. the code has change with new 1.4 version of Kotlin

In which lesson and step of the codelab can this issue be found? Lesson number + step number. (e.g., Lesson 1.1, Step 1.3)

How to reproduce? What are the exact steps to reproduce the problem?

Versions

  1. What version of Android Studio are you using?
  2. What API level are you targeting?

Additional information Add any other context about the problem here.

codelab: advanced-android The code is 👍 package com.productivix.sensorsurvey

import android.R.layout import android.hardware.Sensor import android.hardware.SensorManager import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() { private var mSensorManager: SensorManager? = null

// Individual light and proximity sensors.
private var mSensorProximity: Sensor? = null
private var mSensorLight: Sensor? = null

// TextViews to display current sensor values
private var mTextSensorLight: TextView? = null
private var mTextSensorProximity: TextView? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  /*  mSensorManager = this.getSystemService(SENSOR_SERVICE) as SensorManager?
    val sensorList = mSensorManager?.getSensorList(Sensor.TYPE_ALL)
    val sensorText = StringBuilder()
    if (sensorList != null) {
        for (currentSensor in sensorList) {
            sensorText.append(currentSensor.name).append(
                    System.getProperty("line.separator"))
        }
    }
    val sensorTextView = findViewById<View>(R.id.sensor_list) as TextView
    sensorTextView.text = sensorText */

    mTextSensorLight = findViewById(R.id.label_light)
    mTextSensorProximity = findViewById(R.id.label_proximity)

    mSensorProximity =
            mSensorManager?.getDefaultSensor(Sensor.TYPE_PROXIMITY)
    mSensorLight = mSensorManager?.getDefaultSensor(Sensor.TYPE_LIGHT)

    val sensor_error = resources.getString(R.string.error_no_sensor)
    if (mSensorLight == null) {
        mTextSensorLight?.setText(sensor_error);
    }
    if (mSensorProximity == null) {
        mTextSensorProximity?.setText(sensor_error);
    }

}

}