title: Android - bindService at fragment
body: <p>I have an Activity Class and at it's <code>onResume</code> part I've used the next code -</p>
  
  <pre><code>    @Override
  protected void onResume() {
  
      super.onResume();
      bindService(new Intent(MainActivity.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);
  }
  </code></pre>
  
  <p>At the <code>onPause</code> I've used the next code -</p>
  
  <pre><code>@Override
  protected void onPause() 
  {       
      unbindService(mConnection);
      super.onPause();
  }
  </code></pre>
  
  <p>As you can understand I'm binding and unbinding to a Service.</p>
  
  <p>Now the thing is that I want to use fragment instead of Activity.</p>
  
  <p>In order to do that I've changed the onPuse code into this -</p>
  
  <pre><code>    @Override
  protected void onPause() 
  {       
      getActivity().unbindService(mConnection);
      super.onPause();
  }
  </code></pre>
  
  <p>And it seems fine.</p>
  
  <p>But the thing I have problem with is the bind part at the <code>onResume</code>, I've tried the next code -</p>
  
  <pre><code>            @Override
      protected void onResume() {
  
          super.onResume();
   bindService(new Intent(getActivity(), IMService.class), mConnection , Context.BIND_AUTO_CREATE);
      }
  </code></pre>
  
  <p>But Eclipse gives me an error saying -</p>
  
  <pre><code>The method bindService(Intent, ServiceConnection, int) is undefined for the type MainFragment
  </code></pre>
  
  <p>the service is showing like this at the manifest - </p>
  
  <pre><code>    &lt;service android:name="com.example.test.services.IMService" &gt;
      &lt;/service&gt;
  </code></pre>
  
  <p>So why I can't bind the Service into the fragment? Maybe I need to add something to the getActivity code?</p>
  
  <p>Thnks for any kind of help</p>
  
  <p>Thanks for @Raghunandan help - this is the solution-</p>
  
  <pre><code>getActivity().bindService(new Intent(getActivity(), IMService.class), mConnection , Context.BIND_AUTO_CREATE);
  </code></pre>