outgoing call number

jota

Active Member
Licensed User
Longtime User
:sign0085: I need to know what phone number called a user before the call begins. Use PhoneStateChanged but I have no information of the called number and Calllogs is updated after hanging up the call. On the forum I have seen is a complex issue but to anyone made ​​any progress? thanks
 

jota

Active Member
Licensed User
Longtime User
Also need to know the call time elapsed during the call. Is there any way? thanks
 
Upvote 0

jota

Active Member
Licensed User
Longtime User
I searched online, and it seems that today with the Google API seems impossible. I've only seen code to hang up a call indiscriminately.

Thanks
 
Upvote 0

frla

Member
Licensed User
Longtime User
I found this on the net: java - How to get outgoing call number in android? - Stack Overflow

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.

in my case it was phone state as listed: idle > ringing > offhook > idle in first idle there was extra data with phone number


From my tests we get the offhook event directly. Is it possible to get those other events or is it not possible in Java?
 
Upvote 0

corsaro

Member
Licensed User
Longtime User
I found this "newbies way" to get outgoing call phone number, using broadcastreceiver v2.0 library:

create a service (I named it s11), using these lines of code:

B4X:
Sub Process_Globals
   Dim broadcast As BroadCastReceiver
End Sub


Sub Service_Create
broadcast.Initialize("BroadcastReceiver")
End Sub


Sub Service_Start (StartingIntent As Intent)
  broadcast.addAction("android.intent.action.NEW_OUTGOING_CALL")
    broadcast.SetPriority(2147483647)
    broadcast.registerReceiver("") 
End Sub


Sub BroadcastReceiver_OnReceive (Action As String,i As Object)
 
   
   Dim i2 As Intent
   
   
    i2 = i

   
   Dim X = i2.ExtrasToString
   
   Dim Z = X.IndexOf("NUMBER=")

   Dim y
   y = X.SubString2(Z+7,Z+24)
   y= y.Replace(" ","")
   y= y.Replace(",","") 
   y= y.Replace("a","")
   y= y.Replace("n","")
   y= y.Replace("d","")
   y= y.Replace("r","")
   y= y.Replace("o","")
   y= y.Replace("i","")
   y= y.Replace("d","")
   y= y.Replace(".","")
   
   ToastMessageShow(y,False)
   
   End Sub


on manifest add these lines

B4X:
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.PROCESS_OUTGOING_CALLS)
AddPermission(android.permission.CALL_PHONE)
AddReceiverText(s11, <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
            <category android:name="android.intent.category.DEFAULT" /> 
        </intent-filter>)


So doing, you will be success to get the outgoing call number. On my example, it is y, and it will appear as a toastmessage on screen.



Kindly note: Probably a most experienced user, could try to get the outgoing call number on a "more professional way", directly from the extras intent, using something like

Dim Intent1 As Intent
Dim number As String
Intent1.Initialize(Action, "")
number= Intent1.GetExtra("PHONE_NUMBER")
ToastMessageShow(number,False)

But I was not success using this "more professional" way, due to my newbies status ;)
 
Last edited:
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
You should be familiar with the sms interception sample project first, as indicated by Erel. Then you can try with the interception of an outgoing phone call. I can confirm that Erel advised you the best way to get it work.

You will need to create a service to listen to the new outgoing call and put this working code into your service module.

B4X:
Sub Service_Start(startingIntent As Intent)
   
   If startingIntent.Action = "android.intent.action.NEW_OUTGOING_CALL" Then
      If startingIntent.HasExtra("android.intent.extra.PHONE_NUMBER") Then
         Dim myphone As String
         myphone = startingIntent.GetExtra("android.intent.extra.PHONE_NUMBER")
      
         Log(myphone)         
         
      End If
   End If
   
End Sub

It works for me 100%. See the outgoing call number in the log window.

But before debugging you will need also to add into the manifest editor the following:

B4X:
AddPermission(android.permission.PROCESS_OUTGOING_CALLS)
AddReceiverText(intentsms, <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
            <category android:name="android.intent.category.DEFAULT" /> 
        </intent-filter>)

Don't forget to change the name "intentsms" - which is the name of the service in my project - by the name of the service in your project.
 
Last edited:
Upvote 0

pappicio

Active Member
Licensed User
Longtime User
my question is little different:
it's possible to change number of outgoing call???
for example, if I compose the dial numer:"123456", before phone calls it, I can add to that number a prefix, like:"1234", so, the real call, will dial the composed number:
"1234 123456"?
thanks in advance!!!
 
Upvote 0

pappicio

Active Member
Licensed User
Longtime User
my question is little different:
it's possible to change number of outgoing call???
for example, if I compose the dial numer:"123456", before phone calls it, I can add to that number a prefix, like:"1234", so, the real call, will dial the composed number:
"1234 123456"?
thanks in advance!!!

how to insert this code into b4a???
B4X:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


public class OutgoingCallInterceptor extends BroadcastReceiver {                            // 1

    @Override
    public void onReceive(Context context, Intent intent) {                                 // 2
        final String oldNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);          // 3
        this.setResultData("0123456789");                                                   // 4
        final String newNumber = this.getResultData();
        String msg = "Intercepted outgoing call. Old number " + oldNumber + ", new number " + newNumber;
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

}
 
Upvote 0
Top