1

Possible Duplicate:
launching my app when dialing a number

I would like to get the mobile number from dialer which has dialed by user in my android application.I have implemented an application as follows:

 ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:")));

        }
    });

from the above code i can open dialer app.If user enter a mobile number and click for call then i would like to get which number he has typed. please any body help on it...

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
prasad.gai
  • 2,977
  • 10
  • 58
  • 93
  • 4
    I disagree with the closing of this thread - the "duplicate" is not discussing at all the fetching of the phone number. Furthermore the answer there is with a lot lower value. If you wish to close a thread, close the other one. – Boris Strandjev Dec 30 '12 at 10:00
  • I am also trying something like this. But i want to get the number before the call is made as soon as something is dialed in the dialer app. Can any body help? – Atul O Holic May 21 '14 at 10:23

2 Answers2

9

You can use BroadcastReceiverto do that :

Register a BroadcastReceiver like this in Manifest file:

    <!-- DIAL Receiver -->
    <receiver
        android:exported="true"
        android:name="receivers.DialBroadcastReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

You need Permission for NEW_OUTGOING_CALL :

<!-- OUTGOING CALL PERMISSION-->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

Eventually you can Receive the broadcast and retrieve the dialed number like this:

public class DialBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Log.v("DileBroadCastReceiver","In onReceive()");

    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
         String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

         Log.v("DialBroadcast Receiver","Number is: "+number);

    }

}

}

Sayyam
  • 959
  • 10
  • 22
  • PROCESS_OUTGOING_CALLS is banned (January 2019) since the app is not the default phone calls handler – Duna Oct 21 '19 at 07:31
0

I got my solution as following code

public class OutGoingCall extends BroadcastReceiver {

   @Override
   public void onReceive(final Context context, final Intent intent) 
   {
         // get phone number from bundle
         String phoneNumber = intent.getExtras().getString(OutGoingCall.INTENT_PHONE_NUMBER);

   }
}
prasad.gai
  • 2,977
  • 10
  • 58
  • 93