title: Detect Outgoing call has started and ended
body: <p>I want to detect when the outgoing call has started and ended, and want to start an service when call has started and end the service when call has ended.</p>
  
  <p>Here's the code i got from another question, but don't know where to put the start and stop activity to make it work.</p>
  
  <pre><code>public class CallListener {
  
  private CallStateListener callStateListener = null;
  private OutgoingReceiver outgoingReceiver = null;
  
  public CallListener(Context context) {
  this.context = context;
  
  callStateListener = new CallStateListener();
  outgoingReceiver = new OutgoingReceiver();
  }
  
  private class CallStateListener extends PhoneStateListener {
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
      doSomething1();
      break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
      doSomething2();
      break;
    case TelephonyManager.CALL_STATE_RINGING:
      doSomething3();
      break;
    }
    }
    }
  
    public class OutgoingReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       doSomething4();
   }
   }
  
   public void start() {
  telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
  
  IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
  context.registerReceiver(outgoingReceiver, intentFilter);
   }
  
  public void stop() {
  telephonyManager = (TelephonyManager)       context.getSystemService(Context.TELEPHONY_SERVICE);
  telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
  
  context.unregisterReceiver(outgoingReceiver);
  }}     
  </code></pre>
  
  <p>And here's to add in the service to make it work:</p>
  
  <pre><code>public class MyService extends Service {
  
  private CallListener call = null;
  
  public MyService() {
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
  call = new CallListener(getApplicationContext());
  call.start();
  return(START_STICKY);
  }
  
  @Override
  public void onDestroy() {
  call.stop();
  call.onDestroy();
  }
  
  @Override
  public IBinder onBind(Intent intent) {
  return null;
  }
  }
  </code></pre>
  
  <p>Thanks so much for any help, i may know that this is easy but i just don't know.</p>
  
  <p>Thanks and have a really good day..!! :)</p>