lab9
This commit is contained in:
@@ -45,6 +45,4 @@ dependencies {
|
|||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
androidTestImplementation(libs.androidx.junit)
|
androidTestImplementation(libs.androidx.junit)
|
||||||
androidTestImplementation(libs.androidx.espresso.core)
|
androidTestImplementation(libs.androidx.espresso.core)
|
||||||
|
|
||||||
implementation("org.osmdroid:osmdroid-android:6.1.20")
|
|
||||||
}
|
}
|
||||||
@@ -2,11 +2,6 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
|
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|||||||
@@ -1,33 +1,74 @@
|
|||||||
package com.example.myapplication
|
package com.example.myapplication
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.hardware.Sensor
|
||||||
|
import android.hardware.SensorEvent
|
||||||
|
import android.hardware.SensorEventListener
|
||||||
|
import android.hardware.SensorManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.widget.TextView
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import org.osmdroid.config.Configuration
|
|
||||||
import org.osmdroid.util.GeoPoint
|
|
||||||
import org.osmdroid.views.MapView
|
|
||||||
import org.osmdroid.views.overlay.Marker
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity(), SensorEventListener {
|
||||||
private lateinit var map: MapView
|
|
||||||
|
private lateinit var sensorManager: SensorManager
|
||||||
|
private var accelerometer: Sensor? = null
|
||||||
|
private var lightSensor: Sensor? = null
|
||||||
|
|
||||||
|
private lateinit var tvAccelerometer: TextView
|
||||||
|
private lateinit var tvLight: TextView
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
Configuration.getInstance().load(this, getSharedPreferences("osmdroid", MODE_PRIVATE))
|
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
map = findViewById(R.id.map)
|
tvAccelerometer = findViewById(R.id.tvAccelerometer)
|
||||||
map.setMultiTouchControls(true)
|
tvLight = findViewById(R.id.tvLight)
|
||||||
|
|
||||||
// Координаты Москвы
|
// Получаем доступ к сенсорам
|
||||||
val startPoint = GeoPoint(55.751244, 37.618423)
|
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
||||||
map.controller.setZoom(12.0)
|
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
|
||||||
map.controller.setCenter(startPoint)
|
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)
|
||||||
|
|
||||||
// Маркер
|
// Если сенсор отсутствует
|
||||||
val marker = Marker(map)
|
if (accelerometer == null) {
|
||||||
marker.position = startPoint
|
tvAccelerometer.text = "Акселерометр не поддерживается"
|
||||||
marker.title = "Москва"
|
}
|
||||||
map.overlays.add(marker)
|
if (lightSensor == null) {
|
||||||
|
tvLight.text = "Датчик освещённости не поддерживается"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
accelerometer?.also {
|
||||||
|
sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL)
|
||||||
|
}
|
||||||
|
lightSensor?.also {
|
||||||
|
sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
super.onPause()
|
||||||
|
sensorManager.unregisterListener(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSensorChanged(event: SensorEvent?) {
|
||||||
|
when (event?.sensor?.type) {
|
||||||
|
Sensor.TYPE_ACCELEROMETER -> {
|
||||||
|
val x = event.values[0]
|
||||||
|
val y = event.values[1]
|
||||||
|
val z = event.values[2]
|
||||||
|
tvAccelerometer.text = "Акселерометр:\nX = %.2f\nY = %.2f\nZ = %.2f".format(x, y, z)
|
||||||
|
}
|
||||||
|
Sensor.TYPE_LIGHT -> {
|
||||||
|
val lux = event.values[0]
|
||||||
|
tvLight.text = "Освещённость: $lux люкс"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,18 @@
|
|||||||
<org.osmdroid.views.MapView xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:id="@+id/map"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent" />
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:padding="16dp">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvAccelerometer"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Акселерометр: данные отсутствуют" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvLight"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Освещённость: данные отсутствуют" />
|
||||||
|
</LinearLayout>
|
||||||
Reference in New Issue
Block a user