Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    }
 }
like image 744
Pratik Sule Avatar asked Sep 22 '15 12:09

Pratik Sule


2 Answers

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.

like image 39
RAAAAM Avatar answered Oct 09 '22 13:10

RAAAAM


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);
    }
}
like image 148
Pratik Sule Avatar answered Oct 09 '22 13:10

Pratik Sule