[안드로이드 스튜디오/코틀린] 동작 센서, 와이파이
Motion Sensor
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.shinyelee.android_study2
import android.content.Context
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 com.shinyelee.android_study2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity(), SensorEventListener {
private var vBinding : ActivityMainBinding? = null
private val binding get() = vBinding!!
lateinit var sensorManager: SensorManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
override fun onResume() {
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL)
super.onResume()
}
override fun onPause() {
sensorManager.unregisterListener(this)
super.onPause()
}
override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
}
override fun onSensorChanged(event: SensorEvent?) {
val x = event?.values?.get(0) as Float
val y = event?.values?.get(1) as Float
val z = event?.values?.get(2) as Float
println(x.toString() + ", " + y.toString() + ", " + z.toString())
}
override fun onDestroy() {
vBinding = null
super.onDestroy()
}
}
Wi-fi
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shinyelee.android_study2">
<!-- 인터넷/와이파이 접근 위해 코드 추가 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- 생략 -->
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:text="BUTTON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.shinyelee.android_study2
import android.content.Context
import android.net.ConnectivityManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.shinyelee.android_study2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private var vBinding : ActivityMainBinding? = null
private val binding get() = vBinding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btn.setOnClickListener { NetworkCheck(binding.btn) }
}
fun NetworkCheck(v: View) {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
println(connectivityManager.activeNetworkInfo?.isConnected)
}
override fun onDestroy() {
vBinding = null
super.onDestroy()
}
}
참고
반응형
'개발(Android) > android studio' 카테고리의 다른 글
[Android Studio/Kotlin] Views (0) | 2022.05.28 |
---|---|
[Android Studio/Kotlin] Fragment, Include (0) | 2022.05.18 |
[Android Studio/Kotlin] Volume, Music Streaming (0) | 2022.05.17 |
[Android Studio/Kotlin] Time Picker, Saving Data (0) | 2022.05.17 |
[Android Studio/Kotlin] Dialog, Share (0) | 2022.05.17 |