How to get Outgoing call number with date and time? - android

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();
}
}

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);
}
}

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.

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.

Related

How to make a call in Android and tell whether it was answered

I'd like to make a call to some phone number and, if it was not answered, make a call to an alternative phone number.
Is this possible?
I'm trying this code, but I always get CALL_STATE_IDLE and CALL_STATE_OFFHOOK first (just when the call is made) and then when the call is done (either because the call was not answered or because it finished) I get again CALL_STATE_IDLE.
TelephonyManager manager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener callListener = new MyCallListener();
manager.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phoneNumber));
activity.startActivity(intent);
class MyCallListener extends PhoneStateListener
{
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
if (TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(TAG, "RINGING, number: " + incomingNumber);
} else if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
Log.i(TAG, "OFFHOOK, number: " + incomingNumber);
} else if (TelephonyManager.CALL_STATE_IDLE == state) {
Log.i(TAG, "IDLE, number: " + incomingNumber);
} else {
Log.i(TAG, "Unexpected call state: " + state);
}
}
}
You cannot detect whether your call has been answered or not.
The TelephonyManager.CALL_STATE_RINGING is for incoming call not for out going call.
follow this
public static final int CALL_STATE_RINGING
Added in API level 1
Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.
Constant Value: 1 (0x00000001)

Variable value is null inside TelephonyManager.EXTRA_STATE_OFFHOOK state

I want to perform some operation with calling number on call received or TelephonyManager.EXTRA_STATE_OFFHOOK state. I am getting null value inside the
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){}
My code is as below-
// onReceive function of the Broadcast Receiver
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incommingCall = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context,"Call from "+ incommingCall, Toast.LENGTH_LONG).show();
// value of **incomingcall** is present like..+918000000000. etc
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Toast.makeText(context,"Call from extra_state_offHOOk "+ incommingCall, Toast.LENGTH_LONG).show();
// value of **incomingcall** is null.
}
}
how can I do this?
This means that there is no String extra matching TelephonyManager.EXTRA_STATE in the Intent for the broadcast you've received.
The best way to protect against the null pointer is to rearrange you if statement to:
if(TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
Then, you'll handle it only when TelephonyManager.EXTRA_STATE_OFFHOOK is present.

Android detect missed call

I am attempting to have my application recognize when there is a missed call. I have created a BroadcastReceiver that detects when the phone's state has changed. Here is my onReceive() method:
public void onReceive(Context context, Intent intent) {
boolean ringing = false, callReceived = false;
String callerPhoneNumber = "";
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
ringing = true;
Bundle bundle = intent.getExtras();
callerPhoneNumber= bundle.getString("incoming_number");
if (ringing) {
Toast.makeText(context, "Phone call from: " + callerPhoneNumber, Toast.LENGTH_LONG).show();
}
}
if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
callReceived = true;
Toast.makeText(context, "Call received.", Toast.LENGTH_LONG).show();
}
if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
// Toast.makeText(context, "Phone is idle", Toast.LENGTH_LONG).show();
if (ringing) {
Toast.makeText(context, "ringing is true", Toast.LENGTH_LONG).show();
}
// if (ringing && !callReceived) {
// Toast.makeText(context, "It was A MISSED CALL from : " + callerPhoneNumber, Toast.LENGTH_LONG).show();
// }
}
The issue I am having is the application detects when the phone is ringing, sets ringing to true, and shows the appropriate toast message. However, when the phone's state is idle, it does not enter the if (ringing) statement to show the ringing is true toast message. However, if I comment in the toast message just above it, it will correctly print that the Phone is idle.
Is it possible that ringing is being reset to false somewhere between the phone ringing and the phone becoming idle again?
You can not store any "state" information in a broadcast receiver. If you want to do a comparison old state / new state, I think you can use a service.

How to get the forwarding status and number?

I want to read the phone setting to see if forwarding is enabled or not. And if it is
enabled then I want to get the number that calls are forwarded to. I DO NOT want to use
"PhoneStateListener" because the forwarding can be activated before my app installation.
What is the solution?
You can make a broadcast call and register it and try to get the outgoing call as shown below :
public class CallBroadcastReceiver extends BroadcastReceiver
{
public static String numberToCall;
public void onReceive(Context context, Intent intent) {
Log.d("CallRecorder", "CallBroadcastReceiver::onReceive got Intent: " + intent.toString());
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
numberToCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d("CallRecorder", "CallBroadcastReceiver intent has EXTRA_PHONE_NUMBER: " + numberToCall);
}
}
}

Getting the outbound phonenumber in Android

Bellow a simplified piece of my code is shown. The important part is that I now have phone number when the call state is CALL_STATE_RINGING and the call is inbound. Now I also want the phone number when the call is outbound (so the phone number on the receiving end). What am I missing here?
#Override
public void onCallStateChanged(int state, String number) {
Log.d("BackgroundService", "State: "+ state +" Number: " + number);
}
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_STATE);
Register a broadcast receiver using ACTION_NEW_OUTGOING_CALL. In the onReceive callback function you will know the number of outgoing call
public void onReceive(Context context, Intent intent) {
String phone = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
And offcourse use the permission android.permission.PROCESS_OUTGOING_CALLS

Categories

Resources