From be2fa91facfdc6aca5ad33ebc778ea988d50d4cf Mon Sep 17 00:00:00 2001 From: sipp11 Date: Tue, 1 Oct 2019 18:14:20 +0900 Subject: [PATCH] Try to add locationManager --- app/src/main/AndroidManifest.xml | 2 + .../co/zzyzx/sensorlogger/EndlessService.kt | 73 ++++++++++++++++++- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 15f70c6..e836dcb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,8 @@ + + , + grantResult: IntArray + ) { + when (requestCode) { + PERMISSION_LOCATION -> { + Toast.makeText( + applicationContext, + "${if (grantResult[0] == PackageManager.PERMISSION_GRANTED) "Good" else "Bad"} Permission for locaion", + Toast.LENGTH_LONG + ) + } + } + } + + override fun onLocationChanged(result: Location) { + val txt = "coords: ${result.longitude}, ${result.latitude} - ${result.time}" + log(txt) + + val notification = createNotification(txt) + nm.notify(notificationId, notification) + } + + override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) { + + } + + override fun onProviderEnabled(p0: String?) { + + } + + override fun onProviderDisabled(p0: String?) { + + } private var wakeLock: PowerManager.WakeLock? = null private var isServiceStarted = false @@ -31,6 +78,7 @@ class EndlessService : Service(), SensorEventListener { private lateinit var mSensorManager: SensorManager private lateinit var mAccelerometer: Sensor + private lateinit var mLocationManager: LocationManager override fun onBind(intent: Intent): IBinder? { log("Some component want to bind with the service") @@ -71,6 +119,23 @@ class EndlessService : Service(), SensorEventListener { mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) + if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager + mLocationManager.requestLocationUpdates( + LocationManager.PASSIVE_PROVIDER, + 0L, + 0.toFloat(), + this + ) + } else { +// requestLocationPermission() + log("No location permission!") + } + +// val location = mLocationManager.addNmeaListener { s: String?, l: Long -> +// log("${s} = ${l.toString()}") +// } + } @@ -102,7 +167,7 @@ class EndlessService : Service(), SensorEventListener { launch(Dispatchers.IO) { pingFakeServer() } - delay(1 * 60 * 1000) + delay(1 * 180 * 1000) } log("End of the loop for the service") } @@ -151,8 +216,8 @@ class EndlessService : Service(), SensorEventListener { "[accel] (${accelLin[0].format(3)}, ${accelLin[1].format(3)}, ${accelLin[2].format( 3 )})" - val notification = createNotification(txt) - nm.notify(notificationId, notification) +// val notification = createNotification(txt) +// nm.notify(notificationId, notification) } } }