5

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();
    }
 }
Amit Basliyal
  • 840
  • 1
  • 10
  • 28
Pratik Sule
  • 163
  • 4
  • 18

3 Answers3

4
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);
    }
}
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
Pratik Sule
  • 163
  • 4
  • 18
  • 1
    bro...everything is ok but, what about time?? and date ???, even im also looking for same thing. Im getting outgoing call number but i need exact outgoing call time.Help me out!!! – Narendra Baratam Dec 02 '15 at 06:05
1

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.

RAAAAM
  • 3,378
  • 19
  • 59
  • 108
0

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.

panonski
  • 555
  • 5
  • 10