nawaab saab
nawaab saab

Reputation: 1902

How to get Dialled PhoneNumber?

I want to get the dialled phonenumber

For getting the incomming call number I used this code, it worked fine

number = intent.getStringExtra("incoming_number");

I searched regarding how can I get the dialled number and I got a common code everywhere, and I used this in my program but its giving nullpointer exception, So can anyone tell what mistake I am making or how may I calculate dialled phone number

else if (callstate == TelephonyManager.CALL_STATE_OFFHOOK) {
                    Log.i("state", "offhook state");
                number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

                        Log.i("number", number);
}

In the manifest:

<receiver android:name="Incommingcall" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.CALL_PRIVILEGED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Upvotes: 0

Views: 142

Answers (2)

Noman
Noman

Reputation: 4109

Try this to get all call types:

 private void getCallDetails()
{

  final StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
while (managedCursor.moveToNext())
{
 String phNumber = managedCursor.getString(number);
 String callType = managedCursor.getString(type);
 String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode)
{
case CallLog.Calls.OUTGOING_TYPE:
 dir = "DIALED";
 break;

 case CallLog.Calls.INCOMING_TYPE:
 dir = "RECEIVED";
 break;

 case CallLog.Calls.MISSED_TYPE:
 dir = "MISSED";
 break;
 }
  sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
 sb.append("\n----------------------------------");
 }
managedCursor.close();

 call.setText(sb.toString());

}

}

Upvotes: 0

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

At the time of outgoing call, your intent action will be Intent.ACTION_NEW_OUTGOING_CALL in onReceive

So you can try this:

@Override
public void onReceive(final Context context, final Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
        dialled_num = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    }
}

Refer here for more info.

Hope it helps.

Upvotes: 2

Related Questions