Reputation: 2530
I am developing an application in which i want to keep a track an outgoing call and its duration.For example,If a mobile phone user dials a call I used Intent.ACTION_NEW_OUTGOING_CALL.*When he cut the call,what action to implement?.*I referred all tutorials,they suggested that use device call log URI.But i want to do it in broadcastreceiver.Thanks in advance.
public class MyCallReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
}
}
Upvotes: 1
Views: 874
Reputation: 10829
Try this:
Boolean wasnumberdialed=false;
Boolean callpicked=false;
//if dialed below is called
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
wasnumberdialed=true;
}
//if the call is received by the recipient then this is executed and its an
//outgoing call,(wasnumberdialed is set to true only for outgoing calls) we
//need to ignore incoming calls. If wedont check this an incoming call
//would also execute this.
if (intent.getAction().equals(Intent.CALL_STATE_OFFHOOK && wasnumberdialed)) {
//start your timer to count the time of call
//long start_time = System.currentTimeMillis();
callpicked=true;
}
//after the call is disconnected and was received which is an outgoing call below is
//executed
if (intent.getAction().equals(Intent.EXTRA_STATE_IDLE && callpicked)) {
//end your timer here
//long end_time = System.currentTimeMillis();
}
Log.v("myapp","Total call time is "+TimeUnit.MILLISECONDS.toMinutes(end_time-start_time)+" mins);
Upvotes: 1