This commit is contained in:
wiecktobi 2020-05-27 20:19:05 +02:00
parent 08b996308f
commit 73187542cd

View File

@ -1,13 +1,20 @@
package de.hft.geotracker.activities
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.os.Bundle
import android.os.Looper
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.requestPermissions
import com.google.android.gms.location.*
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingClient
import com.google.android.gms.location.GeofencingRequest
@ -26,6 +33,8 @@ import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
import kotlin.properties.Delegates
class MainActivity : AppCompatActivity() {
@ -34,12 +43,29 @@ class MainActivity : AppCompatActivity() {
lateinit var actionButton: TextView
var running = false
lateinit var service: GeofenceService
lateinit var locationRequest: LocationRequest
lateinit var fusedLocationClient: FusedLocationProviderClient
lateinit var locationCallback: LocationCallback
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
//Get location data and permissions
createLocationRequest()
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED
) {
requestPermissions(this, arrayOf(ACCESS_FINE_LOCATION), 1000)
}
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
}
}
//React on geofence state
this.getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
?.edit()
?.putBoolean("ENABLED", false)
@ -172,6 +198,26 @@ class MainActivity : AppCompatActivity() {
PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
override fun onResume() {
super.onResume()
startLocationUpdates()
}
private fun startLocationUpdates() {
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
}
private fun createLocationRequest() {
locationRequest = LocationRequest.create().apply {
interval = 10000
fastestInterval = 5000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
}
override fun onBackPressed() {
}