How to get outgoing call status on Android phone?

I noticed that the TelephonyManager class has CALL_STATE_IDLE, CALL_STATE_OFFHOOK and CALL_STATE_RINGING. They seem to be used for incoming calls.

What I really want to do is to receive notifications when an outgoing call is received, time is accepted or expires. How to do it?

+5
source share
3 answers

From what I understand, you may find that an outgoing call has been initiated because the state of the phone changes from idle to offhook. However, from there, knowing the state of this call, i.e. Knowing if the call is ringing, whether voicemail is being transmitted, the actually selected one or just the timeout turns out to be something that we cannot detect.

Now I’m not sure that it is simply not detected in the SDK, but is transmitted over the network and, possibly, can be detected from the radio itself or if this information is simply not transmitted.

+1
source

The minimum that needs to be done 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));
        }
        }
}
+1
source

I do not know if you can define a temporary call, but differentiate when a call can be launched.

You can do it like this in CALL_STATE_IDLE:

Uri allCalls = Uri.parse("content://call_log/calls");
String lastMinute = String.valueOf(new Date().getTime() - DAY_IN_MILISECONDS); 
//before the call started
Cursor c = app.getContentResolver().query(allCalls, null, Calls.DATE + " > " 
           + lastMinute, null, Calls.DATE + " desc");
c.moveToFirst();

if (c.getCount() > 0) {
    int duration = Integer.parseInt(c.getString(c.getColumnIndex(Calls.DURATION)));
}

if the duration is> 0, then the answer was triggered.

Obviously, there are other flags that you should use to determine that CALL_STATE_IDLE is called after the call has been made.

Hope this helps and puts you in the correct position for what you are trying to do.

+1
source

All Articles