title: How to pass Activity to a service
body: <p>How do i pass an activity as parameter for a spesific service :</p>
  
  <p>here is the service : </p>
  
  <p>for each 5 seconds i want this action to occure: (for messanger updte)</p>
  
  <pre><code>        channel_id = intent.getStringExtra("channelID");
          if (MapWithMarkers.channels_map.get(channel_id).getChannel_messages().size()
                  &gt; ChatActivity.last_given_size_of_messages) {
              if (activity != null) {
  
                  ((ChatActivity) activity).updateNewMessagesOnScreen(); // THIS MUST ACCEPT ACTIVITY!!!
              }
  
  
          }
  </code></pre>
  
  <p>now i want to call this from the activity that calls the service:</p>
  
  <pre><code>public void updateNewMessagesOnScreen() {
      List&lt;MessageItem&gt; messages = MapWithMarkers.channels_map.get(channelId).getChannel_messages();
      int max_size = messages.size();
  
      for (int i = last_given_size_of_messages; i &lt; max_size; i++) {
          if (messages.get(i).getUser_id().equals("me")) {
              adp.add(new ChatMessage(false, messages.get(i).getText()));
          } else {
              adp.add(new ChatMessage(true, messages.get(i).getText()));
          }
      }
      last_given_size_of_messages = max_size;
  }
  
  package com.ap2.demo;
  
  import android.app.Activity;
  import android.app.Service;
  import android.content.ComponentName;
  import android.content.Intent;
  import android.os.Binder;
  import android.os.IBinder;
  
  import com.ap2.demo.enumPackage.Constants;
  
  import java.util.Timer;
  import java.util.TimerTask;
  
  public class ChatService extends Service {
      private Timer timer = new Timer();
      String channel_id ="";
      Activity activity = null;
      // Binder given to clients
      private final IBinder mBinder = new LocalBinder();
  
      @Override
      public void onCreate() {
          // code to execute when the service is first created
          int x = 10;
  
      }
  
      @Override
      public void onDestroy() {if (timer != null) {
              timer.cancel();
          }
      }
  
      @Override
      public int onStartCommand(final Intent intent, int flags, int startid) {
  
          timer.scheduleAtFixedRate(new TimerTask() {
  
              @Override
              public void run() {
                  channel_id = intent.getStringExtra("channelID");
                  // Check if there are updates here and notify if true
                  // new ServerSamples().execute(Constants.SERVER_REQUESTS.GET_SERVERS);
                  if (MapWithMarkers.channels_map.get(channel_id).getChannel_messages().size()
                          &gt; ChatActivity.last_given_size_of_messages) {
                      if (activity != null) {
                          ((ChatActivity) activity).updateNewMessagesOnScreen();
                      }
  
  
                  }
  
  
              }
          }, 0, 1000);
          return START_STICKY;
      }
  
  
      /**
       * Class used for the client Binder.  Because we know this service always
       * runs in the same process as its clients, we don't need to deal with IPC.
       */
      public class LocalBinder extends Binder {
          ChatService getService() {
              // Return this instance of LocalService so clients can call public methods
              return ChatService.this;
          }
      }
  
      @Override
      public IBinder onBind(Intent intent) {
  
          return mBinder;
      }
      /** method for clients */
      public void setActivity (Activity activity, String channel_id) {
          this.activity = activity;
          // Check if there are updates here and notify if true
  
          if (MapWithMarkers.channels_map.get(channel_id).getChannel_messages().size()
                  &gt; ChatActivity.last_given_size_of_messages) {
              if (activity != null) {
                  ((ChatActivity) activity).updateNewMessagesOnScreen();
              }
          }
      }
  
      private void stopService() {
          if (timer != null) timer.cancel();
      }
  
  
  }
  </code></pre>
  
  <p>in my activity:</p>
  
  <pre><code> @Override
      protected void onResume() {
          super.onResume();
          // Bind to ChatService
          Intent intent = new Intent(ChatActivity.this, ChatService.class);
          bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
  
      }
  
      @Override
      protected void onStop() {
          super.onStop();
          // Unbind from the service
          if (mBound) {
              unbindService(mConnection);
              mBound = false;
  
          }
      }
      /** Defines callbacks for service binding, passed to bindService() */
      private ServiceConnection mConnection = new ServiceConnection() {
  
          @Override
          public void onServiceConnected(ComponentName className,
                                         IBinder service) {
              // We've bound to LocalService, cast the IBinder and get LocalService instance
          ChatService.LocalBinder binder = (ChatService.LocalBinder) service;
          chatService = binder.getService();
          chatService.setActivity(ChatActivity.this, channelId);
          mBound = true;
  
      }
  
      @Override
      public void onServiceDisconnected(ComponentName arg0) {
          mBound = false;
      }
  };
  </code></pre>