Pratik Sule
Pratik Sule

Reputation: 163

How to get Outgoing call number with date and time?

I am getting Incoming call Details(Number,Name,Date). But How to get outgoing call details. I have written a code for outgoing calls details but it throws NullPointerException. Below is my MyCallReceiver.java file and manifest file

  public void onReceive(Context context, Intent intent) {

    this.context = context;



    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {

        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

        Toast.makeText(context, "Call From : " + incomingNumber, Toast.LENGTH_LONG).show();

        doToast(getContactName(context, incomingNumber) + " " + incomingNumber);
        String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
        doToast(currentDateTimeString +" "+incomingNumber);


    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        Toast.makeText(context, "DETECTED CALL HANG UP EVENT", Toast.LENGTH_LONG).show();

        String outgoingNumber=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Toast.makeText(context,"Calling To :"+outgoingNumber,Toast.LENGTH_LONG).show();
    }
 }

Upvotes: 5

Views: 7481

Answers (3)

Pratik Sule
Pratik Sule

Reputation: 163

public void onReceive(Context context, Intent intent)
{
    String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);

    if(state==null)
    {
        //Outgoing call
        String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Log.i("tag","Outgoing number : "+number);
    }
    else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {
        //Incoming call
        String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.i("tag","Incoming number : "+number);
    }
}

Upvotes: 4

panonski
panonski

Reputation: 555

Creating separate listeners for different events (ie. incoming vs outgoing calls) might make your life easier. For outgoing calls, instead checking for state of TelephonyManager, you might want to create an IntentFilter.

IntentFilter filterCalls = new IntentFilter();
filterCalls.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
MyCallReceiver myCallReceiver = new MyCallReceiver();
registerReceiver(myCallReceiver, filterCalls);

Then in your onReceive you can simply have:

 String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)

and be sure that it will work.

Upvotes: 0

RAAAAM
RAAAAM

Reputation: 3380

First of all intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER) gives you outgoing number while phone state is idle and turns to "null" while phone status changes to OFF_HOOK.

The easiest way was to save the number before another onRecive happens.

Upvotes: 1

Related Questions