Android Question How to log both incoming and outgoing calls?

Horatio Trobinson

New Member
Licensed User
Longtime User
I have this java code and I don't seem to be able to do the same in B4A in a straightforward way. I looked around in this forum but I get sparse bits of (sometimes contradictory) information, and not a single complete solution that I can tell. It's well possible that I'm still not very good at navigating forums, but I promise I will get better at it in due time :)

I need to:
Log both incoming and outgoing calls, then passing the incoming and outgoing numbers to an activity that can display the numbers or save them somewhere for later use.
Thank you in advance for any help you can provide.

Here's the java code I have:
IncomingCallReceiver.java
B4X:
package org.vruz.calllog.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class IncomingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();       
        if(null == bundle)
            return;
        Log.i("IncomingCallReceiver",bundle.toString());
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);
        Log.i("IncomingCallReceiver","Call: "+ state);
       
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
        {
            String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.i("IncomingCallReceiver","Incoming number: " + phonenumber);
            String info = "Incoming call from: " + phonenumber;
            Toast.makeText(context, info, Toast.LENGTH_LONG).show();
        }
    }

}

OutgoingCallReceiver.java

B4X:
package org.vruz.calllog.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class OutgoingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if(null == bundle)
            return;
        String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Log.i("OutgoingCallReceiver",phonenumber);
        Log.i("OutgoingCallReceiver",bundle.toString());
        String info = "Calling number: " + phonenumber;
        Toast.makeText(context, info, Toast.LENGTH_LONG).show();
    }
}
 

bsnqt

Active Member
Licensed User
Longtime User
Intercepting incoming call and outgoing call is quite easy with Broadcast Receiver in B4A. Check out this library in the forum, you will get some sample codes as well. You can use also PhoneStateListener, PhoneEvents (like rbsoft mentioned)... as well as using of Static intents. Below is example
B4X:
Sub BroadcastReceiver_OnReceive (Action As String, BroadcastIntent As Object)
  myintent = BroadcastIntent
  If myintent.Action = "android.intent.action.PHONE_STATE" Then
     If myintent.GetExtra("state") = "RINGING" Then
        callernumber = myintent.GetExtra("incoming_number")
     End if
   End If

   If myintent.Action = "android.intent.action.NEW_OUTGOING_CALL" Then
     If myintent.HasExtra("android.intent.extra.PHONE_NUMBER") Then
         callnumber = myintent.GetExtra("android.intent.extra.PHONE_NUMBER")
     End If
   End If
End Sub
 
Upvote 0

Horatio Trobinson

New Member
Licensed User
Longtime User
Thanks, @rbsoft and @bsnqt.
@bsnqt: That Broadcast Receiver library is helpful as it should work in a regular (non-service) app.
(For future reference in case anybody reads this in the future, here's --> the BroadcastReceiver Library)
I did another test using AddReceiverText in a manifest (a static receiver) and monitoring intents in a service, but I think your solution suits me better in my current project.
I'll run some tests and report back. Thanks again, guys!
 
Upvote 0
Top