Search code examples
androidphone-calltelephonymanageroutgoing-call

How to detect an Outgoing call in android


Just I want to know if there is a code that can detect if 'im calling someone with the default android phone app.. this is my code here and of course I need more hints

if(intent.getAction().equals("android.intent.action.PHONE_STATE")){
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            call = true;
            Log.v("OUTGOING", String.valueOf(call));
            //Do-NOTHING
        } else {
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                if (call == false) {
                    Log.v("OUTGOING", String.valueOf(call));
                    Log.v("CALL", "Call from " + numberPhone);
                    numberPhone = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                }
            }
        }
}

Solution

  • public class OutgoingReceiver extends BroadcastReceiver {
     public OutgoingReceiver() {
     }
    
     @Override
     public void onReceive(Context context, Intent intent) {
         String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    
         Toast.makeText(ctx, 
           "Outgoing: "+number, 
           Toast.LENGTH_LONG).show();
     }
    

    And set Receiver

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

    Add Permission

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

    you have to register the intent

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
    ctx.registerReceiver(outgoingReceiver, intentFilter);