Browse Source

Try to add locationManager

master
sipp11 5 years ago
parent
commit
be2fa91fac
  1. 2
      app/src/main/AndroidManifest.xml
  2. 73
      app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt

2
app/src/main/AndroidManifest.xml

@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application <application
android:allowBackup="true" android:allowBackup="true"

73
app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt

@ -3,15 +3,21 @@ package co.zzyzx.sensorlogger
import android.app.* import android.app.*
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color import android.graphics.Color
import android.hardware.Sensor import android.hardware.Sensor
import android.hardware.SensorEvent import android.hardware.SensorEvent
import android.hardware.SensorEventListener import android.hardware.SensorEventListener
import android.hardware.SensorManager import android.hardware.SensorManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Build import android.os.Build
import android.os.Bundle
import android.os.IBinder import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import android.provider.Settings import android.provider.Settings
import android.support.v4.app.ActivityCompat
import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationCompat
import android.widget.Toast import android.widget.Toast
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -19,9 +25,50 @@ import java.util.*
import com.github.kittinunf.fuel.Fuel import com.github.kittinunf.fuel.Fuel
//import com.github.kittinunf.fuel.core.extensions.jsonBody //import com.github.kittinunf.fuel.core.extensions.jsonBody
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.util.jar.Manifest
class EndlessService : Service(), SensorEventListener { const val PERMISSION_LOCATION = 0
class EndlessService : Service(), SensorEventListener, LocationListener,
ActivityCompat.OnRequestPermissionsResultCallback {
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
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 wakeLock: PowerManager.WakeLock? = null
private var isServiceStarted = false private var isServiceStarted = false
@ -31,6 +78,7 @@ class EndlessService : Service(), SensorEventListener {
private lateinit var mSensorManager: SensorManager private lateinit var mSensorManager: SensorManager
private lateinit var mAccelerometer: Sensor private lateinit var mAccelerometer: Sensor
private lateinit var mLocationManager: LocationManager
override fun onBind(intent: Intent): IBinder? { override fun onBind(intent: Intent): IBinder? {
log("Some component want to bind with the service") log("Some component want to bind with the service")
@ -71,6 +119,23 @@ class EndlessService : Service(), SensorEventListener {
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) 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) { launch(Dispatchers.IO) {
pingFakeServer() pingFakeServer()
} }
delay(1 * 60 * 1000) delay(1 * 180 * 1000)
} }
log("End of the loop for the service") 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( "[accel] (${accelLin[0].format(3)}, ${accelLin[1].format(3)}, ${accelLin[2].format(
3 3
)})" )})"
val notification = createNotification(txt) // val notification = createNotification(txt)
nm.notify(notificationId, notification) // nm.notify(notificationId, notification)
} }
} }
} }

Loading…
Cancel
Save