roko_man

LocationService

Oct 29th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.59 KB | None | 0 0
  1. ///////////////////////////////////// Implemintation to initialize and Stop Service/////////////////////////////////////////////////
  2. context.startService(Intent(this,LocationService()::class.java));
  3. context.stopService(Intent(this,LocationService()::class.java));
  4.  
  5. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6.  
  7. ////////////////////////////////////////////////////////////Location Service//////////////////////////////////////////////////////////
  8. class LocationService : Service()
  9. {
  10.     var gpsTracker : GpsTracker? = null;
  11.     var context : Context? = null;
  12.  
  13.     override fun onBind(intent: Intent): IBinder?
  14.     {
  15.         return null
  16.     }
  17.  
  18.     override fun onCreate()
  19.     {
  20.         try{
  21.  
  22.             context = this;
  23.             Log.e("Service","Service Created");
  24.             gpsTracker = GpsTracker(this, object : OnLocationUpdated
  25.             {
  26.                 override fun locationChange(location: Location)
  27.                 {
  28.                     sendLocationToSocket(location);
  29.                 }
  30.  
  31.             });
  32.  
  33.         } catch (e: Exception){
  34.  
  35.         }
  36.     }
  37.  
  38.  
  39.     private fun sendLocationToSocket(location:Location)
  40.     {
  41.         if(gpsTracker != null && gpsTracker!!.ifICanGetLocation() && location != null)
  42.         {
  43.             Log.e("LocationLat","${location.latitude}");
  44.             Log.e("LocationLon","${location.longitude}");
  45.         }
  46.     }
  47. }
  48. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment