/* package whatever; // don't place package name! */ import android.app.Service import android.content.Context import android.content.Intent import android.location.Location import android.location.LocationManager import android.os.Bundle import android.os.IBinder import android.util.Log import com.napoleonit.monita.utilities.log.MLog /** * Created by user on 03.05.16. */ private val TAG: String = "BOOMBOOMTESTGPS" private var mLocationManager: LocationManager? = null private val LOCATION_INTERVAL: Long = 10000 private val LOCATION_DISTANCE: Float = 0f class MyService : Service() { class LocationListener(gpS_PROVIDER: String) : android.location.LocationListener { var mLastLocation: LocationListener? = null fun LocationListener(provider: String) { MLog.d(TAG, "LocationListener $provider"); var mLastLocation = Location(provider); } override fun onLocationChanged(location: Location?) { MLog.d(TAG, "onLocationChanged: $location"); mLastLocation?.onLocationChanged(location); } override fun onProviderEnabled(provider: String) { MLog.d(TAG, "onProviderEnabled: " + provider); } override fun onProviderDisabled(provider: String) { MLog.d(TAG, "onProviderDisabled " + provider); } override fun onStatusChanged(provider: String, status: Int, extras: Bundle) { MLog.d(TAG, "onStatusChanged " + provider); } } var mLocationListeners = arrayOf( LocationListener (LocationManager.GPS_PROVIDER), LocationListener (LocationManager.NETWORK_PROVIDER)) override fun onBind(arg0 : Intent): IBinder? { return null; } override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { MLog.d(TAG, "onStartCommand"); super.onStartCommand(intent, flags, startId); return START_STICKY; } override fun onCreate() { MLog.d(TAG, "onCreate"); initializeLocationManager(); try { mLocationManager?.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, mLocationListeners[1]); } catch (ex: java.lang.SecurityException) { Log.i(TAG, "fail to request location update, ignore", ex) } catch (ex: IllegalArgumentException) { MLog.d(TAG, "network provider does not exist ${ex.message}") } try { mLocationManager?.requestLocationUpdates( LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, mLocationListeners[0]) } catch (ex: java.lang.SecurityException) { Log.i(TAG, "fail to request location update, ignore", ex) } catch(ex: IllegalArgumentException) { Log.d(TAG, "gps provider does not exist ${ex.message}") } } override fun onDestroy() { var y: Int = 0 MLog.d(TAG, "onDestroy") super.onDestroy() if (mLocationManager != null) { for (i:LocationListener in mLocationListeners) { try { mLocationManager.removeUpdates(mLocationListeners[y]) y++ } catch (ex: Exception) { Log.i(TAG, "fail to remove location listeners, ignore", ex) } } } } private fun initializeLocationManager() { Log.e(TAG, "initializeLocationManager"); if (mLocationManager == null) { mLocationManager = applicationContext.getSystemService(Context.LOCATION_SERVICE) as (LocationManager) } } }