Question

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.

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.

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);
}}     

And here's to add in the service to make it work:

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;
}
}

Thanks so much for any help, i may know that this is easy but i just don't know.

Thanks and have a really good day..!! :)

Was it helpful?

Solution

I had already searched how to detect call events like ringing / idle / etc... no success :( Unfortunately, there is no solution in Android at this time...

This is well explained in this previous question/summary post:

Detecting outgoing call answered on Android

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top