I noticed in the class TelephonyManager there are CALL_STATE_IDLE, CALL_STATE_OFFHOOK and CALL_STATE_RINGING. They seem to be used for incoming calls.
What I actually want to do is to be notified when an outgoing call is made, is received, or timed out. How to do that?
Navigate to Restrictions -> Phone. Under Calls, find the Outgoing calls option and click on Restrict to block outgoing calls on Android devices. Next, Save and Publish the restrictions profile. It is recommended to test the profile on a test device before associating it with your production environment.
You need a BroadcastReceiver for ACTION_PHONE_STATE_CHANGED This will call your received whenever the phone-state changes from idle,ringing,offhook so from the previous value and the new value you can detect if this is an incoming / outgoing call.
I don't know if you can detect a timed call, but differentiate when the call started is possible.
You can do it like this, in the 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 duration is > 0 then then it call was answered.
Obviously there are other flags that you should use to determine that CALL_STATE_IDLE is called after a call was made.
Hope that helps and put you in the corret way for what you are trying to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With