title: how to pass data between service and it's application in the right way?
body: <p>i'm a newbie in android. In my app i create a many-to-many chat, and need to update from server a list of Messages. In order to do so, i created a service that updates every second from the server. </p>
  
  <p>My problem is that i don't know how to pass data back to the application. I know that I should do it using intent and broadcast receiver, but in that I stuck with Bundle object that i have to serialize in order to pass it to the app, and it does not make sense to me, since this operation is not that efficient.</p>
  
  <p>For now i'm using the ref to my application (i think it's not that good but don't know why), and after every update from server in the service i activate the application function, and updates it's fields directly. Moreover i think maybe my code will do some good for beginners as well :)</p>
  
  <pre><code>public class UpdateChatService extends Service {
  
  private static final long DELAY_FOR_CHAT_TASK = 0;
  private static final long PERIOD_FOR_CHAT_TASK = 1;
  private static final TimeUnit TIME_UNIT_CHAT_TASK = TimeUnit.SECONDS;
  
  //private Task retryTask; TODO: check this out
  private ScheduledExecutorService scheduler;
  
  private boolean timerRunning = false;
  private long RETRY_TIME = 200000;
  private long START_TIME = 5000;
  
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
  
      scheduleChatUpdate();
  }
  
  private void scheduleChatUpdate() {
      BiggerGameApp app = (BiggerGameApp) getApplication();
      this.scheduler = Executors.newScheduledThreadPool(3);
      this.scheduler.scheduleAtFixedRate(new UpdateChatTask(app), 
              DELAY_FOR_CHAT_TASK, PERIOD_FOR_CHAT_TASK,
              TIME_UNIT_CHAT_TASK);
      timerRunning = true;
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      if (!timerRunning) {
          scheduleChatUpdate();
      }
  
      return super.onStartCommand(intent, flags, startId);
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
  
      if (scheduler != null) {
          scheduler.shutdown();
      }
  
      timerRunning = false;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the code of the asynchronous task the runs in the service. 
  Please tell me what i'm doing wrong, and how should pass data from the service to the application.</p>
  
  <pre><code>public void run() {
      try {
          if (this.app.getLastMsgFromServer() == null) {
              this.app.setLastMsgFromServer(new Message(new Player(DEFAULT_EMAIL), "", -1));
              this.app.getLastMsgFromServer().setMessageId(-1);
          }
  
          Gson gson = new GsonBuilder()
          .registerTypeAdapter(DateTime.class, new DateTimeTypeConverter())
          .create();
          ServerHandler serverHandler = new ServerHandler();
  
          String jsonString = gson.toJson(this.app.getLastMsgFromServer());
  
          // Sending player to servlet in server
          String resultString = serverHandler.getResultFromServlet(jsonString, "GetListOfMessages");      
  
          if (resultString.contains("Error")) {
              return;
          }
  
          // Parsing answer
          JSONObject json = new JSONObject(resultString);
          Status status = null;
          String statusString = json.getString("status");    
  
          if (statusString == null || statusString.length() == 0)
              return;
  
          status = Status.valueOf(statusString);
  
          if (Status.SUCCESS.equals(status)) {       
              ArrayList&lt;Message&gt; tempChat = null;
  
              JSONArray jsonList = json.getJSONArray("data");
              MyJsonParser jsonParser = new MyJsonParser();
              tempChat = jsonParser.getListOfMessagesFromJson(jsonList.toString());    
  
              if (tempChat != null &amp;&amp; tempChat.size() != 0) {
                  // After getting the chat from the server, it saves the last msg
                  // For next syncing with the server
                  this.app.setLastMsgFromServer(tempChat.get(LAST_MSG_INDEX));
  
                  tempChat.addAll(this.app.getChat());
  
                  if (tempChat.size() &gt; SIZE_OF_USER_CHAT) {
                      tempChat = (ArrayList&lt;Message&gt;) tempChat.subList(0, SIZE_OF_USER_CHAT - 1);
                  }
  
                  this.app.setChat(tempChat);
                  this.app.updateViews(null);
              }
          }
  
          return;
  </code></pre>