The minimum that is need to do is:
public class CallCounter extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("Tony","Outgoing Call finished");
// Call Finished -> stop counter and store it.
callStop=new Date().getTime();
context.stopService(new Intent(context,ListenerContainer.class));
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("Tony","Outgoing Call Starting");
// Call Started -> start counter.
// This is not precise, because it starts when calling,
// we can correct it later reading from call log
callStart=new Date().getTime();
break;
}
}
public class ListenerContainer extends Service {
public class LocalBinder extends Binder {
ListenerContainer getService() {
return ListenerContainer.this;
}
}
@Override
public void onStart(Intent intent, int startId) {
TelephonyManager tManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
CallCounter callCounter=new CallCounter(this);
tManager.listen(callCounter,PhoneStateListener.LISTEN_CALL_STATE);
Log.d("Tony","Call COUNTER Registered");
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
}
public class myReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
context.startService(new Intent(context,ListenerContainer.class));
}
}
}