예제 #1
0
 /** send user location to server */
 public void sendLocation(double lat, double lon) {
   if (connection != null && connection.isConnected()) {
     JSONObject json = new JSONObject();
     try {
       json.put("lon", lon);
       json.put("lat", lat);
     } catch (JSONException e) {
       SLog.e(TAG, e.getMessage());
     }
     String message = json.toString();
     SLog.d(TAG, "trying to send: " + message);
     connection.sendTextMessage(message);
   }
 }
예제 #2
0
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
   // allow to use service when device is inactive
   WakeLock wakelock =
       ((PowerManager) getSystemService(POWER_SERVICE))
           .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SocketServiceLock");
   wakelock.acquire();
   SLog.d(TAG, "onStartCommand");
   if (intent != null) {
     SLog.d(TAG, intent.toUri(0));
   }
   shutDown = false;
   // initialize connection
   if (connection == null || (!connection.isConnected() && !isConnecting)) {
     connectionWakeLock =
         ((PowerManager) getSystemService(POWER_SERVICE))
             .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SocketService_clientLock");
     connection = new WebSocketConnection();
     try {
       isConnecting = true;
       connection.connect(
           String.format(
               Consts.SERVER_URL, Settings.getUserLogin(this), Settings.getUserPassword(this)),
           new WebSocketHandlerImpl());
       // wake lock will be released in connection listener;
     } catch (WebSocketException e) {
       SLog.e(TAG, e.getMessage());
       if (connectionWakeLock != null && connectionWakeLock.isHeld()) {
         connectionWakeLock.release();
       }
     }
     // start shutting down
   } else if (intent != null) {
     if (ACTION_SHUT_DOWN.equals(intent.getAction())) {
       shutDown = true;
       if (connection.isConnected()) connection.disconnect();
     }
   }
   wakelock.release();
   return START_STICKY;
 }