title: How can handle in better way the handler message from service to fragment?
body: <p>For communicate from started service to fragment I did in this way :</p>
  
  <p><strong>In the service body</strong> </p>
  
  <pre><code>    Handler handler;
  
  
  public synchronized void setHandler(Handler h)
  {
      handler = h;
  }
  
  protected synchronized void sendMessage(int arg1)
  {
      Message m = handler.obtainMessage();
      m.arg1=arg1;
      handler.sendMessage(m);
  }
  </code></pre>
  
  <p>In the fragment :</p>
  
  <pre><code>@Override
      public void onServiceConnected(ComponentName name, IBinder service) {
  
          LocalBinder binder = (LocalBinder) service;
          mService = ("service_name") binder.getService();
          mService.setHandler(serviceHandler);
  
  
      }
  </code></pre>
  
  <p>and :</p>
  
  <pre><code>private Handler serviceHandler = new Handler(Looper.myLooper()) 
      {
              @Override
              public void handleMessage(Message msg) 
              {
                  someF(msg);
              }
      };
  
      private void someF(Message msg)
          {
              //some functions
          }
  </code></pre>
  
  <p>In onDestroy I have did in this way :</p>
  
  <p>Now in fragment's ondestroy I do this :</p>
  
  <pre><code>if (mIsBound) {
                  // Detach our existing connection.
                  App.getAppContext().unbindService(mConnection);
                  mIsBound = false;
              }
  </code></pre>
  
  <p>Function to bind and unbind from the service :</p>
  
  <pre><code>void doBindService() {
  
      Appbike.getAppContext().bindService(new Intent(getActivity(), Bike_ble_service.class), mConnection,Context.BIND_AUTO_CREATE);
      mIsBound = true;
  
  }
  
  /**
   * Detach our existing connection
   */
  void doUnbindService() {
      if (mIsBound) {
          Appbike.getAppContext().unbindService(mConnection);
          mIsBound = false;
      }
  }
  </code></pre>
  
  <p><strong>edit2</strong> : Now In the service I used  </p>
  
  <pre><code>public class LocalBinder extends Binder {
          LocalService getService() {
              // Return this instance of LocalService so clients can call public methods
              return LocalService.this;
          }
      }
   @Override
      public IBinder onBind(Intent intent) {
          return mBinder;
      }
  </code></pre>
  
  <p>And in fragment I can call service function through the binder. </p>
  
  <p>How can resolve my problem ? If I do unbindservice the message from the service continue to arrives ..</p>
  
  <p>Do I have to change everything ?</p>
  
  <p><strong>SOLUTION</strong> : I try to do this in the service :</p>
  
  <pre><code>@Override
  public IBinder onBind(Intent intent) {
      iHaveToSendMsg = true;
      return mBinder;
  }
  
  @Override
      public boolean onUnbind(Intent intent)
      {
          iHaveToSendMsg = false;
          return true;
      }
  </code></pre>
  
  <p>and in sendmessage I check the flag, if it is false I don't send message. Is a possible workaround ?</p>
  
  <p><strong>MY SOLUTION'S PROBLEM</strong> : "<em>In my solution there is one problem... Onbind isn't called everytime that I do a bindService :/ What can I do ?
  I can't rewrite all the code and use callback, for this time I have to use handler message.. :(</em> "</p>
  
  <p><strong>POSSIBLE SOLUTION</strong> : can I set the flag in setHandler() function</p>
  
  <pre><code>public synchronized void setHandler(Handler h)
      {
          iHaveToSendMsg = true;
          handler = h;
      }
  </code></pre>