Getting the outbound phonenumber in Android - 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

Related

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

ACTION_PHONE_STATE_CHANGED not called on network cell changes

I am trying to make my broadcast receiver fire when the phone goes in and out of reception areas. The issue is the receiver never gets called when the cell reception changes. The BroadcastReceiver works fine for getting phone call states (call idle, started ect...), and also for getting the airplane mode switched on and off because the broadcast receiver handles both.
I added the permissions and intent filter to the receiver in the manifest and they are working fine.
Here is what I have for my BroadcastReceiver and PhoneStateListener.
public class PhoneStateReceiver extends BroadcastReceiver {
private PhoneStateListener mListener = new ServiceStateListener();
private TelephonyManager mTelephonyManager;
private Context mContext;
/**
* TODO add some sort of call back interface to allow for different uses of this phone state receiver
*/
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
mContext = context;
if (action.intern().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
if (!isAirplaneModeOn) {
SmsRetryManager.getInstance().retryAllSms(context);
}
} else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
mTelephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
Toast.makeText(mContext, "Receiver registered!", Toast.LENGTH_LONG).show();
mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_SERVICE_STATE);
mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
}
public void onDestroy() {
mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_NONE);
}
private class ServiceStateListener extends PhoneStateListener {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
super.onServiceStateChanged(serviceState);
boolean connected = (serviceState.getState() == ServiceState.STATE_IN_SERVICE);
if (connected) {
Toast.makeText(mContext, "Connection Gained!", Toast.LENGTH_LONG).show();
//todo retry sms here
SmsRetryManager.getInstance().retryAllSms(mContext);
} else {
Toast.makeText(mContext, "Connection Lost!", Toast.LENGTH_LONG).show();
}
}
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
Toast.makeText(mContext, "Signal changed - cdma : " + signalStrength.getCdmaDbm() + " gsm : " + signalStrength.getGsmSignalStrength(), Toast.LENGTH_LONG).show();
}
}
Any insight would be awesome. I have been banging my head on this one for a while.
Thanks!
I assume you are listening the broadcast android.intent.action.SERVICE_STATE
If so, try using:
if (TelephonyManager.getnetworkOperator.length()==0) connected=false;
on your OnReceive method, to know if the phone is not connected. It works fine for me.
If this solution doesn't work, please show how you register the receiver: is it statically registered at manifest.xml? or dynamically with PackageManager.setComponentEnabledSetting? Note: With static registration you'll find the receiver is not triggered after reinstalling the app, needing to add to the receiver tag
<intent-filter>
<action android:name= "android.intent.action.MY_PACKAGE_REPLACED"/></intent-filter>
You can also look at the values that return the ServiceState.
See here http://developer.android.com/reference/android/telephony/ServiceState.html
And check which of these values is returned:
int STATE_EMERGENCY_ONLY = The phone is registered and locked. Only
emergency numbers are allowed.
int STATE_IN_SERVICE = Normal operation condition, the phone is registered with an operator either in home network or in roaming.
int STATE_OUT_OF_SERVICE = Phone is not registered with any operator,
the phone can be currently searching a new operator to register to, or
not searching to registration at all, or registration is denied, or
radio signal is not available.
int STATE_POWER_OFF = Radio of telephony is explicitly powered off.

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

How to determine the phone number of a current caller in a stand-alone application

I'd like to build an Android application that can contact the current caller via a pre-determined text message. Sending a text message is simple enough but determining the phone number of the current caller in a stand-alone application is the challenge. Is the there an easy way to divine the phone number so I can send them a message while still on the call?
Of course there are manual ways to do this: write down the number, key it into a new text message, enter the message. But I want to define the message up front and be able to "send it to current caller".
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
helper = new ContactDatabaseHelper(context);
list = helper.getAllContacts();
try{
incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (list.size() != 0){
for ( int i = 0, size = list.size(); i < size; i++ ){
if (PhoneNumberUtils.compare(incomingNumber, list.get(i).getContactNumber())){
ToastMsg.showToast(context,list.get(i).getContactName()+" Calling");
}
}
}
}catch (Exception e) {
// TODO: handle exception
}
}
public class PhoneCallStateListener extends PhoneStateListener{
private Context context;
public PhoneCallStateListener(Context context){
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
break;
case PhoneStateListener.LISTEN_CALL_STATE:
}
super.onCallStateChanged(state, incomingNumber);
}
}
For your sistuation the best I can think of is to use PhoneStateListener. It contains onCallStateChanged handler. One of the arguments is a String containing the incoming phone number.
Source:
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
Ctrl + F and type in "Incoming" and you will find everything you need to know.
EDIT: To make sure you're app starts on the startup of your phone, just add a BroadcastReciever. How to start an Application on startup?
Register a BroadcastReceiver in your manifest that listens to ACTION_PHONE_STATE_CHANGED.
Broadcast intent action indicating that the call state (cellular) on
the device has changed.
The EXTRA_STATE extra indicates the new call state. If the new state
is RINGING, a second extra EXTRA_INCOMING_NUMBER provides the incoming
phone number as a String.
Requires the READ_PHONE_STATE permission.
This was a sticky broadcast in version 1.0, but it is no longer
sticky. Instead, use getCallState() to synchronously query the current
call state.
This way you don't need the user to launch your app before receiving a call.

How to track android phone call after dial the call button and before ringing in Android application

I want to track the phone call information after dialing the call button and before ringing.Means before going call to the person whom I want to call, I want to a notification in my application.
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Try to read the phone number from previous receivers.
String phoneNumber = getResultData();
if (phoneNumber == null) {
// We could not find any previous data. Use the original phone number in this case.
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
}
}
You can retrieve this from a Broadcast in response to ACTION_NEW_OUTGOING_CALL:
Register a BroadcastReceiver with an ACTION_NEW_OUTGOING_CALL intent filter. You can also read this Android Developers blog post regarding some details on the subject.

Categories

Resources